NSUserDefault 存入NSMutableArray数组解决方案

如果想使用NSUserDefault存入NSMutableArray 首先你得先做已下步凑:(注意:NSMutableArray 里面add的是一个实体对象)

1、 NSMutableArray里面的实体对象必须要从写

-(void)encodeWithCoder:(NSCoder *)aCoder

{

[aCoder encodeObject: self .name forKey:@ "name" ];
    [aCoder encodeObject:[ NSNumber numberWithInt: self .age ] forKey:@ "age" ];
    [aCoder encodeObject:self.gay forKey:@ "gay" ];

}

2、接下来就是在 NSUserDefault 里面存数据

NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];

//需要将你需要存储的数据转成data

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:yourObject];

[currentDefaults setObject:data forKey:@"yourKeyName"];


3、接下来就是在 NSUserDefault 里面取数据

NSData *data = [currentDefaults objectForKey:@"yourKeyName"];

yourObjectType * obj = [NSKeyedUnarchiver unarchiveObjectWithData:data];


4、接下来就是在 NSUserDefault 里面移除数据

[currentDefaults removeObjectForKey:@"yourKeyName"];


你可能感兴趣的:(NSUserDefault 存入NSMutableArray数组解决方案)