我正在以这种方式向 NSuserdefults 读取和写入数组:
indexDelete = button.tag;
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
NSData *dataRepresentingSavedArray = [currentDefaults objectForKey:@"save"];
if (dataRepresentingSavedArray != nil)
{
NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
if (oldSavedArray != nil)
myIntegers = [[NSMutableArray alloc] initWithArray:oldSavedArray];
else
myIntegers = [[NSMutableArray alloc] init];
}
[myIntegers addObject:[NSNumber numberWithInteger:indexDelete]];
NSLog (@"Array myIntegers: %@", myIntegers);
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:myIntegers] forKey:@"save"];
如果我保持这条线,一切都会正常工作:
myIntegers = [[NSMutableArray alloc] init];
括号外,因为即使数据在那里,也没有正确写入或读取:
我收到此错误:-[NSKeyedUnarchiver initForReadingWithData:]: data is NULL
我做错了什么??