[英]How to store my customer NSMutableArray in NSUserDefaults for Objective-C?
I am trying to store my customer data into NSUserDefaults
. 我正在尝试将客户数据存储到
NSUserDefaults
。 The customer data is like the following: 客户数据如下:
AvinLightInfo.h 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
In view.m 在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];
I try to use the following code to store the above data in NSUserDefaults
. 我尝试使用以下代码将以上数据存储在
NSUserDefaults
。
[[NSUserDefaults standardUserDefaults] setObject:AvinLightDataBase forKey:@"AvinLightDataBase"];
[[NSUserDefaults standardUserDefaults] synchronize];
But it crash and show the following error log: 但它崩溃并显示以下错误日志:
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'
How to store my customer NSMutableArray
in NSUserDefaults
for Objective-C
? 如何将我的客户
NSMutableArray
存储在Objective-C
NSUserDefaults
中?
-------------------------EDIT------------------------------- - - - - - - - - - - - - -编辑 - - - - - - - - - - - - -------
I also try the following method: 我也尝试以下方法:
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:AvinLightDataBase];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:data forKey:@"light"];
[defaults synchronize];
But it crash and the error log is : 但是它崩溃了,错误日志是:
-[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:
You can store an array of custom object by the following way 您可以通过以下方式存储自定义对象的数组
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:yourArray];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:data forKey:@"yourKey"];
NSArray *array= [NSKeyedUnarchiver unarchiveObjectWithData:[defaults objectForKey:@"yourKey"];
For more detail you can follow this tutorial Archiving Objective-C Objects with NSCoding 有关更多详细信息,您可以按照本教程使用NSCoding归档Objective-C对象
NSUserDefault can only support NSNumber(NSInteger、float、double),NSString,NSDate,NSArray,NSDictionary,BOOL, So, if you want to store customer data, you should convert you customer data to NSData, and your customer object must be implement NSCoding, and override encodeWithCoder and initWithCoder. NSUserDefault仅支持NSNumber(NSInteger,float,double),NSString,NSDate,NSArray,NSDictionary,BOOL,因此,如果要存储客户数据,则应将客户数据转换为NSData,并且客户对象必须实现NSCoding ,并覆盖encodeWithCoder和initWithCoder。
And then, please pay attention to your code style, the variable will be start with lower case. 然后,请注意您的代码样式,该变量将以小写字母开头。
Use this Third party RMMApper 使用此第三方RMMApper
To store use 存放使用
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults rm_setCustomObject:AvinLightDataBase forKey:@"key"];
To retrive 取回
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *object = [defaults rm_customObjectForKey:@"key"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.