0

I am trying to store my customer data into NSUserDefaults. The customer data is like the following:

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

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 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 ?

-------------------------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:
Wun
  • 6,211
  • 11
  • 56
  • 101
  • http://stackoverflow.com/questions/19720611/attempt-to-set-a-non-property-list-object-as-an-nsuserdefaults – aNa12 Dec 27 '16 at 08:47

3 Answers3

1

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

Usman Javed
  • 2,437
  • 1
  • 17
  • 26
  • Hi , I have try and edit in the above, but it crash. Did I missing something ? – Wun Dec 27 '16 at 09:23
  • Have you implemented the initWithCode method? – Usman Javed Dec 27 '16 at 09:32
  • Note that "values returned from NSUserDefaults are immutable, even if you set a mutable object as the value." [Apple Docs](https://developer.apple.com/reference/foundation/nsuserdefaults?language=objc) So, if you want to get back your original _NSMutableArray_, the retrieval should look like: `NSMutableArray *array= [[NSKeyedUnarchiver unarchiveObjectWithData:[defaults objectForKey:@"yourKey"] mutableCopy];` . – degapps Dec 27 '16 at 10:03
  • what issue is this? Just go ahead and look you got your previous stored array or not. – Usman Javed Dec 27 '16 at 10:08
  • Missing a closing ] bracket in the unarchive call. – nickdnk Sep 08 '18 at 16:24
1

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.

And then, please pay attention to your code style, the variable will be start with lower case.

Finder丶Tiwk
  • 345
  • 2
  • 8
0

Use this Third party 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"];
vp2698
  • 1,783
  • 26
  • 28