我无法使用NSUserDefaults
使可变对象数组持久化。我已经尝试了几天不同的代码而且无法正确使用它。我已经阅读了有关该主题的所有内容,包括StackOverflow中的所有帖子。我知道我很接近,但我真的很感激有人指出我做错了什么。我知道以下代码中的arr
计数为零,这就是我遇到障碍的地方。我没有超过这一点。我打赌有人会笑,并在阅读我的帖子后2分钟内指出我的愚蠢错误。
以下是相关代码,包括日志:
Contacts.h:
@interface Contacts : NSObject // <NSCoding>
{
NSMutableArray *arr; // 8/21/13
}
Contacts.m
- (void)encodeWithCoder:(NSCoder *)aCoder
{
NSLog(@"In encodeWithCoder");
NSLog(@"In encodeWithCoder. arr's count is: %i", [arr count]);
{
for (int i = 0; i <[arr count]; i++)
{
NSLog(@"In encodeWithCoder %@", [arr objectAtIndex:i]);
NSLog(@"class %@", [arr class]);
}
[aCoder encodeObject:arr forKey:@"allContacts"];
}
}
- (id) initWithCoder:(NSCoder *)aDecoder
{
NSLog(@"In initWithCoder");
arr = [aDecoder decodeObjectForKey:@"allContacts"];
return self;
}
// The following two methods might not even be needed. I copied them from some examples.
- (NSData *)dataOftype:(NSString *)typeName error:(NSError **)outError
{
return nil;
}
- (BOOL)readFromData:(NSData *)data oftype:(NSString *)typeName error: (NSError **)outError
{
arr = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(@"allContacts is ->%@", [arr mutableCopy]);
return YES;
}
ContactStore.h
@interface ContactsStore : NSObject <NSCoding>
{
NSMutableArray *arr; // 8/21/13
NSMutableArray *allContacts;
}
ContactStore.m
- (void)fetchContactsIfNecessary
{
NSLog(@"in ContactsStore.m - fetchContactsIfNecessary #1");
if (!allContacts)
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:@"allContacts"];
allContacts = [[NSMutableArray alloc]initWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:data]];
for (int i = 0; i <[allContacts count]; i++)
{
NSLog(@"In FetchContactsIfNecessary- Gettingfrom NSUserDefaults %@", [allContacts objectAtIndex:i]);
NSLog(@"In FetchContactsIfNecessary- Gettingfrom NSUserDefaults - class %@", [allContacts class]);
}
}
// If we didn't find one in NSUserDefaults, then create a new one.
if (!allContacts)
{
allContacts = [[NSMutableArray alloc] init];
NSLog(@"In FetchContactsIfNecessary-We just allocated allContacts");
}
NSLog(@"in ContactsStore.m - Leaving fetchContactsIfNecessary2");
}
- (BOOL)saveChanges
{
for (int i = 0; i <[allContacts count]; i++)
{
NSLog(@"In saveChanges %@", [allContacts objectAtIndex:i]);
NSLog(@"In saveChanges class %@", [allContacts class]);
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
arr = allContacts; // 8/21/13
NSLog(@"In saveChanges. arr's count is: %i", [arr count]);
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:arr];
[defaults setObject:data forKey:@"allContacts"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"We just synchronized allContacts - I think");
return 1;
}
日志
2013-08-22 14:13:50.764 ImOK[16979:907] In saveChanges Scott Stringer, (212) 888-1234
2013-08-22 14:13:50.772 ImOK[16979:907] In saveChanges class __NSArrayM
2013-08-22 14:13:50.775 ImOK[16979:907] In saveChanges Sheldon Silver, (212) 987-4321
2013-08-22 14:13:50.781 ImOK[16979:907] In saveChanges class __NSArrayM
2013-08-22 14:13:50.793 ImOK[16979:907] In saveChanges. arr's count is: 2
2013-08-22 14:13:50.795 ImOK[16979:907] In encodeWithCoder
2013-08-22 14:13:50.796 ImOK[16979:907] In encodeWithCoder. arr's count is: 0
2013-08-22 14:13:50.798 ImOK[16979:907] In encodeWithCoder
2013-08-22 14:13:50.799 ImOK[16979:907] In encodeWithCoder. arr's count is: 0
2013-08-22 14:13:50.805 ImOK[16979:907] We just synchronized allContacts - I think
应用程序从后台返回后,名称和电话号码显示正确,但重新启动应用程序后,我只获得了四个空值。我预计,因为方法arr
中encodeWithCoder
的计数为零,但我无法弄清楚如何解决此问题。
答案 0 :(得分:0)
您只对对象进行编码和解码。你还没有存储它。要存储它,您需要这样的功能:
- (BOOL)storeBrandOnDevice
{
NSString *path = [self itemArchivePath];
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:self forKey:@"Contacts"];
[archiver finishEncoding];
BOOL dataSaveSuccess = [data writeToFile:path atomically:YES];
return dataSaveSuccess;
}
从商店获取这样的东西: - (void)getBrandFromStorage {
NSString *path = [self itemArchivePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
NSData *data = [[NSMutableData alloc] initWithContentsOfFile:path];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
Contacts* storedContacts = [unarchiver decodeObjectForKey:@"Contacts"];
[unarchiver finishDecoding];
}
}
这是你缺少的功能:
- (NSString*)itemArchivePath
{
@try {
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
return [documentDirectory stringByAppendingPathComponent:@"items.archive"];
}
@catch (NSException *exception) {
return nil;
}
}
这可能并不完美,但这是一般概念。
有关NSCoding的更多信息,请关注Ray Wenderlich的tutorial