gpt4 book ai didi

cocoa - NSMutableDictionary setObject :forKey - custom class

转载 作者:行者123 更新时间:2023-12-03 18:00:10 27 4
gpt4 key购买 nike

我在 UINavigation 环境中使用它。我有customClassA。它继承customClassB,它的对象之一是 NSMutableDictionary。

我在 viewController 中分配并初始化 customClassA,然后为了添加数据,我将一个新的 viewController 插入堆栈。 addNewDataViewController 发送新添加的数据,即由其委托(delegate)返回的 customClassB 对象。到目前为止一切正常。

customClassA 必须使用键(从 NSDate 创建的 NSString)将返回的对象 (customClassB) 存储到其 NSMutableDictionary 对象中。

我收到“变异方法发送到不可变对象(immutable对象)”错误,并且想不出任何解决方案。如有任何帮助,我们将不胜感激。

谢谢。

============================

interface customClassA : NSObject
{
NSDate date;
NSArray *array; // will contain only NSString objects
}
// and the rest as customary
...

#import "customClassA.h"
interface customClassB : NSObject
{
NSString *title;
NSMutableDictionary *data; // will contain values of customClassA with keys of NSString
}
// and the rest as customary

...

#import "customClassB"
#interface firstViewController : UITableViewController <SecondViewControllerDelegate>
- (void)viewDidLoad
{
self.customClassB_Object = [customClassB alloc] init];
// and the rest...
}

- (void)secondViewControllerDidSaveData:(customClassA *)aData
{
[self.customClassB_Object.data setObject:aData forKey:[NSString stringWithFormat:@"%@", aData.date]];
// update tableView
}

最佳答案

确保您使用类似的内容初始化NSMutableDictionary

NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];

您的 NSMutableDictionary 似乎是使用 NSDictionary 实例而不是 NSMutableDictionary 创建的

关于cocoa - NSMutableDictionary setObject :forKey - custom class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7852094/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com