お客様のデータをNSUserDefaults
に保存しようとしています。私は上記のデータを保存するために、次のコードを使用しようと私の顧客NSMutableArrayをNSUserDefaultsのObjective-Cに保存するにはどうすればいいですか?
AvinLightInfo.h 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];
で
#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
:顧客データは、以下のようなものです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'
Objective-C
ためNSUserDefaults
で私の顧客NSMutableArray
を格納する方法は?
------------------------- 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:
http://stackoverflow.com/questions/19720611/attempt-to-set-a -non-property-list-object-as-an-nsuserdefaults – aNa12