简体   繁体   English

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

[英]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.

相关问题 如何将NSMutableArray存储到Objective-C中的另一个NSMutableArray - How to store an NSMutableArray to another NSMutableArray in Objective-C Objective-C NSUserDefaults不会为特定键保存NSMutableArray - Objective-C NSUserDefaults don't save NSMutableArray for specific key 如何将NSMutableArray添加到NSMutableArray Objective-c - How to add an NSMutableArray to an NSMutableArray Objective-c 如何在Objective-c中将结构保存到NSUserDefaults? - How to save a struct to NSUserDefaults in Objective-c? 如何在NSMutableArray中对对象进行分组并在Objective-C中划分为单独的NSMutableArray - How to group object in NSMutableArray and divide to separate NSMutableArray in Objective-C 如何在NSUserDefaults中存储UILabel的NSMutableArray - How to store NSMutableArray of UILabel in NSUserDefaults objective-C 中的 NSMutableArray 插入 - NSMutableArray insertion in objective-C NSmutablearray是如何存储在NSmutableDictionary目标c中 - NSmutablearray is how to store in NSmutableDictionary objective c 如何使用Objective-C使用两个NSMutableArray填充Expandable TableView - How to populate, Expandable TableView with two NSMutableArray using objective-c 如何在Objective-C中将NSMutableArray的元素设置为UILabel? - How to set the elements of NSMutableArray to UILabel in Objective-C?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM