在 NSUserDefaults 中存储自定义对象
我正在尝试在 NSUserDefaults 中保存一个对象,但我没有成功。场景是,我有一个 SyncObjct 类的对象syncObject。我在 SyncObject 类中实现了以下方法
- (NSMutableDictionary*)toDictionary;
+ (SyncObject*)getFromDictionary :(NSMutableDictionary*)dictionary;
- (id) initWithCoder: (NSCoder *)coder;
- (void) encodeWithCoder: (NSCoder *)coder;
,并且 SyncObject 类是 NSCoding 类。现在 SyncObject 类包含一个名为 jobsArray 的 NSMutableArray。该数组包含 JobsObject 类的对象。我也为此类实现了上述相同的方法,以使其编码兼容。在 SyncObject 类的 toDictionary 方法中,我通过编写以下行将此数组存储在字典中。
[dictionary setValue:jobsArray forKey:@"jobsArray"];
我通过编写以下行在 SyncOject 类的 getFromDictionary 方法中检索它。
+ (SyncObject*)getFromDictionary :(NSMutableDictionary*)dictionary
{
NSLog(@"SyncObject:getFromDictionary");
SyncObject *syncObject = [[SyncObject alloc] init];
@try
{
syncObject.noOfJobs = [[dictionary valueForKey:@"noOfJobs"] intValue];
syncObject.totalJobsPerformed = (TotalJobsPerformed*)[dictionary valueForKey:@"totalobsPerformed"];
syncObject.jobsArray = [dictionary valueForKey:@"jobsArray"];
}
@catch (NSException * e)
{
NSLog(@"EXCEPTION %@: %@", [e name], [e reason]);
}
@finally
{
}
return syncObject;
}
我还通过编写以下几行将syncObject存储在NSUserDefault中。
NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:syncObject];
[userStorage setObject:myEncodedObject forKey:SYNC_OBJECT];
我通过编写以下几行从 NSUserDefaults 检索对象
NSData *myEncodedObject = [userStorage objectForKey: SYNC_OBJECT];
syncObject = (SyncObject*)[NSKeyedUnarchiver unarchiveObjectWithData: myEncodedObject];
问题是我无法正确检索 jobsArray。返回一些无效的对象,并且应用程序在尝试访问它时崩溃。请问谁能告诉我这个问题的原因吗?
此致
I am trying to save an object in NSUserDefaults but i have not been successful in it. Scenario is that, I have an object syncObject of class SyncObjct. I have implemented following methods in SyncObject class
- (NSMutableDictionary*)toDictionary;
+ (SyncObject*)getFromDictionary :(NSMutableDictionary*)dictionary;
- (id) initWithCoder: (NSCoder *)coder;
- (void) encodeWithCoder: (NSCoder *)coder;
And also the SyncObject class is NSCoding class. Now SyncObject class contains an NSMutableArray with name jobsArray. This array contains objects of class JobsObject. I implemented same above methods for this class as well to make it coding compliant. In toDictionary method of SyncObject class, i am storing this array in dictionary by writing following line.
[dictionary setValue:jobsArray forKey:@"jobsArray"];
and I am retrieving it in getFromDictionary method of SyncOject class by writing following line.
+ (SyncObject*)getFromDictionary :(NSMutableDictionary*)dictionary
{
NSLog(@"SyncObject:getFromDictionary");
SyncObject *syncObject = [[SyncObject alloc] init];
@try
{
syncObject.noOfJobs = [[dictionary valueForKey:@"noOfJobs"] intValue];
syncObject.totalJobsPerformed = (TotalJobsPerformed*)[dictionary valueForKey:@"totalobsPerformed"];
syncObject.jobsArray = [dictionary valueForKey:@"jobsArray"];
}
@catch (NSException * e)
{
NSLog(@"EXCEPTION %@: %@", [e name], [e reason]);
}
@finally
{
}
return syncObject;
}
And also I am storing syncObject in NSUserDefault by writing following lines.
NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:syncObject];
[userStorage setObject:myEncodedObject forKey:SYNC_OBJECT];
I am retrieving the object from NSUserDefaults by writing following lines
NSData *myEncodedObject = [userStorage objectForKey: SYNC_OBJECT];
syncObject = (SyncObject*)[NSKeyedUnarchiver unarchiveObjectWithData: myEncodedObject];
Problem is that i am not able to retrieve jobsArray correctly. Some invalid object is returned and app crashes when trying to access it. Please can anyone tell me the reason of this problem?
Best Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为问题在于 NSUserDefaults 不支持存储自定义对象类型。
使用 -setObject 时的 Apple 文档: value 参数只能是属性列表对象:NSData、NSString、NSNumber、NSDate、NSArray 或 NSDictionary。对于 NSArray 和 NSDictionary 对象,它们的内容必须是属性列表对象。
您可以直接从 Apple 的 NSUserDefault 类参考手册。
I believe the problem is that NSUserDefaults does not support storing of custom object types.
From Apple's documentation when using -setObject: The value parameter can be only property list objects: NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. For NSArray and NSDictionary objects, their contents must be property list objects.
You can review the remaining documentation directly from Apple's NSUserDefault Class Reference manual.
存储的对象中存储和检索存储的数据:
Stored your object :