我有一个名为的自定义类ServerModule
这是一个子类NSObject
。我基本上存储了所有这些ServerModule
s 中包含一个键值对NSMutableDictionary
。然后字典存储在NSUserDefaults
。我了解到NSUserDefaults
仅在访问对象时返回对象的不可变版本,因此我将字典初始化更改为:
_AllModules = [[NSMutableDictionary alloc]initWithDictionary:[_editServerModules objectForKey:@"AllModules"]]; //initialize a copy of AllModules dictionary
现在,我只是想存储一个自定义ServerModule
该字典中的对象,并同步它。以下代码尝试执行此操作:
//Create new ServerModule
ServerModule* newServer = [[ServerModule alloc]initWithUUID];
newServer.name = self.tf_name.text;
newServer.ip = self.tf_ip.text;
newServer.port = self.tf_port.text;
newServer.username = self.tf_user.text;
newServer.password = self.tf_pass.text;
//Add the ServerModule to AllModules dictionary with the key of its identifier
[_AllModules setObject:newServer forKey:newServer.identifier];
[self updateData];
[_editServerModules synchronize];
标识符是一个字符串,在构造函数中设置ServerModule
。这是代码updateData
.
[_editServerModules setObject:_AllModules forKey:@"AllModules"];
如果您想知道,该对象位于@"AllModules"
在AppDelegate中初始化如下:
NSMutableDictionary* AllModules = [[NSMutableDictionary alloc]init];
再次,这是我尝试保存某些内容时遇到的错误:
Attempt to set a non-property-list object {
"42E9EEA0-9051-4E2A-81EA-DC8FC5639C26" = "<ServerModule: 0x8ac4e50>";
} as an NSUserDefaults value for key AllModules
谢谢你的帮助!
〜地毯嘶嘶声