如何在NSUserDefaults中为Objective-C存储我的客户NSMutableArray?

时间:2016-12-27 08:37:23

标签: ios objective-c nsmutablearray nsuserdefaults

我正在尝试将客户数据存储到NSUserDefaults。客户数据如下:

AvinLightInfo.h

#import <Foundation/Foundation.h>
@interface AvinLightInfo : NSObject

@property (nonatomic)uint16_t Address;
@property (strong,nonatomic)NSString* name;
@property (strong,nonatomic)NSMutableArray* ColorTempInfo;
@property (strong,nonatomic)NSMutableArray* BrightnessInfo;

@end

view.m

NSMutableArray *AvinLightDataBase = [[NSMutableArray alloc] init];

AvinLightInfo *Avinlightinfo = [[AvinLightInfo alloc] init];
Avinlightinfo.Address = CFSwapInt16(0x3711);
Avinlightinfo.name = @"TEST";

Avinlightinfo.ColorTempInfo = [[NSMutableArray alloc] init];
Avinlightinfo.BrightnessInfo = [[NSMutableArray alloc] init];
NSNumber* defaultValue = [NSNumber numberWithInt:200];
for(int j = 0 ; j < 4 ; j++){
   [Avinlightinfo.ColorTempInfo addObject:defaultValue];
   [Avinlightinfo.BrightnessInfo addObject:defaultValue];
}
[AvinLightDataBase addObject:Avinlightinfo];

我尝试使用以下代码将上述数据存储在NSUserDefaults中。

[[NSUserDefaults standardUserDefaults] setObject:AvinLightDataBase forKey:@"AvinLightDataBase"];
[[NSUserDefaults standardUserDefaults] synchronize];

但它崩溃并显示以下错误日志:

2016-12-27 16:35:23.491723 MESH[2458:992363] [User Defaults] Attempt to set a non-property-list object (
    "<AvinLightInfo: 0x1702414a0>"
) as an NSUserDefaults/CFPreferences value for key AvinLightDataBase
2016-12-27 16:35:23.492929 MESH[2458:992363] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object (
    "<AvinLightInfo: 0x1702414a0>"
) for key AvinLightDataBase'

如何将NSMutableArray中的客户NSUserDefaults存储到Objective-C

------------------------- EDIT --------------------- ----------

我也尝试以下方法:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:AvinLightDataBase];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:data forKey:@"light"];
[defaults synchronize];

但它崩溃了,错误日志是:

-[AvinLightInfo encodeWithCoder:]: unrecognized selector sent to instance 0x17405c8c0
2016-12-27 17:20:54.203510 MESH[2481:998134] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AvinLightInfo encodeWithCoder:]: unrecognized selector sent to instance 0x17405c8c0'
*** First throw call stack:

3 个答案:

答案 0 :(得分:1)

您可以通过以下方式存储自定义对象数组

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:yourArray];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:data forKey:@"yourKey"];


NSArray *array= [NSKeyedUnarchiver unarchiveObjectWithData:[defaults objectForKey:@"yourKey"];

有关详细信息,请参阅本教程Archiving Objective-C Objects with NSCoding

答案 1 :(得分:1)

NSUserDefault只能支持NSNumber(NSInteger,float,double),NSString,NSDate,NSArray,NSDictionary,BOOL, 因此,如果要存储客户数据,则应将客户数据转换为NSData,并且客户对象必须实现NSCoding,并覆盖encodeWithCoder和initWithCoder。

然后,请注意您的代码风格,变量将以小写字母开头。

答案 2 :(得分:0)

使用此第三方RMMApper

存储使用

NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults rm_setCustomObject:AvinLightDataBase forKey:@"key"];

要追溯

NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *object = [defaults rm_customObjectForKey:@"key"];