Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 目标C属性在使用后被重置为空_Ios_Objective C_Iphone - Fatal编程技术网

Ios 目标C属性在使用后被重置为空

Ios 目标C属性在使用后被重置为空,ios,objective-c,iphone,Ios,Objective C,Iphone,在我的根视图控制器(名为rootViewController.h)中,我有一个属性 @property (nonatomic, strong) NSDictionary *contactList; 我正试图在这本字典里储存价值。我将属性传递给我的函数 AddContact *addContact = [[AddContact alloc]init]; NSString *result = @""; result = [addContact addContact:user :[self cont

在我的根视图控制器(名为rootViewController.h)中,我有一个属性

@property (nonatomic, strong) NSDictionary *contactList;
我正试图在这本字典里储存价值。我将属性传递给我的函数

AddContact *addContact = [[AddContact alloc]init];
NSString *result = @"";
result = [addContact addContact:user :[self contactList]];
当我转到AddContact函数和断点时,即使我使用此代码在contactList上添加了5x,contactList仍然为null

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;
根据规则,如果您使用的是可变对象,则需要alloc,init它

因此,在view的didLoad方法中

_contactList = [[NSMutableDictionary alloc] init] ;

您必须初始化联系人列表。您尚未初始化联系人列表,因此未设置该值。如果以后想更改值,可以使用可变字典。请确保添加到contactList的值不为空,否则它们将不会添加到contactList,因此字典中不会有任何对象。

[addContact addContact:user:[self contactList]];此函数的格式似乎不正确,是否可以粘贴函数定义行?已添加函数定义。定义行如
-(void)addContact:(User*)User FromDicit:(NSDictionary*)dict
您的定义是这样的吗?为什么要使用
setValue:forKey: