在我的根视图控制器(名为rootViewController.h)中,我有一个属性
@property (nonatomic, strong) NSDictionary *contactList;
我正试图在这本字典中存储值。我将属性传递给我的函数
AddContact *addContact = [[AddContact alloc]init];
NSString *result = @"";
result = [addContact addContact:user :[self contactList]];
当我转到AddContact函数和断点时,contactList为null,尽管我使用此代码在contactList上添加了5x。
User *newUser = [[User alloc]init];
newUser.name = user.name;
newUser.phoneNumber = user.phoneNumber;
newUser.companyName = user.companyName;
[contactList setValue:newUser forKey:newUser.name];
帮助
如果您想更改NSMutableDictionary
,请尝试使用它。初始化后无法编辑NSDictionary
。
如果你想更改你的字典,你需要创建的是像一样可变的
@property (nonatomic, strong) NSMutableDictionary *contactList;
根据规则,如果您使用的是需要分配的可变对象,请初始化
因此在view的didLoad方法中
_contactList = [[NSMutableDictionary alloc] init] ;
您必须初始化contactList。您尚未初始化contactList,因此未设置该值。如果以后想更改值,可以使用可变字典。请确保添加到contactList的值不是null,否则它们不会添加到contactList,因此字典中不会有任何对象。