假设您的数组中包含的对象符合NSCoding
协议,您可以使用
// let's take the NSString as example
NSArray *array = @[@"foo", @"bar"];
[[NSUserDefaults standardUserDefaults] setObject: [NSKeyedArchiver archivedDataWithRootObject:array] forKey:@"annotationKey"];
NSArray *archivedArray = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"annotationKey"]] ;
如果它们符合NSCopying
协议,那么
// let's take the NSString as example
NSArray *array = @[@"foo", @"bar"];
[[NSUserDefaults standardUserDefaults] setObject: array forKey:@"annotationKey"];
NSArray *archivedArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"annotationKey"] ;
编辑
好的,可能它不适用于NSArray
符合NSCopying
协议的对象,但可以说它适用于符合NSCoding
协议的对象,正如 Brad Larson 在以下答案中指出的那样:
在 NSUserDefaults 的 NSMutableArray 中存储自定义对象