我在Nsuserdefaults中存储了一个NsMutableArray,我认为这可以完成并且正确,所以我将一个文本从TexField添加到Array但是当我尝试在NSLOG中读取它时发送null,继承代码:
@property(nonatomic, strong)NSMutableArray *tasks;
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
[standardUserDefaults setObject:self.tasks forKey:@"tasks"];
[self.tasks addObject:textField.text];
NSLog(@"tasks:%@", [[NSUserDefaults standardUserDefaults]objectForKey:@"tasks"]);
谢谢!
答案 0 :(得分:2)
在将文本保存为默认值
之前,需要将文本添加到数组中NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
// make sure self.tasks is initialized
if (!self.tasks) self.tasks = [NSMutableArray new];
// add object first
[self.tasks addObject:textField.text];
// now save array
[standardUserDefaults setObject:self.tasks forKey:@"tasks"];
NSLog(@"tasks:%@", [[NSUserDefaults standardUserDefaults]objectForKey:@"tasks"]);
如果返回null,则