NSMutableArray not saved NSUserDefault

◇◆丶佛笑我妖孽 提交于 2020-01-06 18:12:02

问题


I'm trying to save a NSMutableArray with NSUSerDefault and then open the array. Here some code:

-(IBAction)btnSave{    
Class *aClass = [[Class alloc]init];    
aClass.idClass=@"aaaxxx";    
aClass.nameClass=@"hello";
[myArray addObject:aClass];
NSUserDefaults *arrayDefault=[NSUserDefaults standardUserDefaults];
[arrayDefault setObject:myArray forKey:@"savedArray"];
[arrayDefault synchronize]; 
}

and

- (void)viewDidLoad
{
    [super viewDidLoad];
    myArray=[[NSMutableArray alloc]init];

    NSMutableArray *savedArray=[[NSUserDefaults standardUserDefaults]objectForKey:@"savedArray"];
    if(savedArray!=NULL){
        myArray=savedArray;
        [tableView reloadData];
    }
    // Do any additional setup after loading the view from its nib.
}

when I compile and when I push the button, this is what I read on log output:

[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value '(
    "<Class: 0x8452b40>"
)' of class '__NSArrayM'.  Note that dictionaries and arrays in property lists must also contain only property values

and obviously when I reopen the view the array is not loaded. any help?


回答1:


NSUserDefaults allows only Premitive DataTypes to be store in it. if you want to store your custom class Object then use following code, for more Detail refer this IOS Documentation

//create an array with your custom class objects

Class *aClass = [[Class alloc]init];
aClass.idClass=@"aaaxxx";
aClass.nameClass=@"hello";

[myArray addObject:aClass];

//convert your array to `NSData` object using `NSKeyedArchiver`    
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:myArray];

//store it to `NSUserDefaults`
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"myArray"];
[[NSUserDefaults standardUserDefaults] synchronize];

//convert your stored Object back to `NSData` using `NSKeyedUnarchiver`    
NSData *storedData = [[NSUserDefaults standardUserDefaults] objectForKey:@"myArray"];
NSArray *storedArr = [NSArray arrayWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:storedData]];

NSLog(@"%@",storedArr[0]);



回答2:


First you implement NSCoding in you object class. The in order to save/load the NSMutableArray of those objects you can do:

- (void) loadArray {
    //Loading the NSMutableArray
    NSData *arrayData = [[NSUserDefaults standardUserDefaults] objectForKey:@"ArrayKey"];
    myArray = [NSKeyedUnarchiver arrayData];
}

-(void) saveArray {
    //Saving the NSMutableArray
    NSData *arrayData = [NSKeyedArchiver archivedDataWithRootObject:myArray];
    [[NSUserDefaults standardUserDefaults] setObject:arrayData forKey:@"ArrayKey"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

For more info I suggest looking at a longer tutorial. For example Ray Wenderlich's How to Save your App Data with NSCoding And NSFileManager is a good one.




回答3:


You need to implement NSCoding protocol in your model object Class. It is needed to serialize and deserialize non standard data to NSUserDefaults.

Now you can use NSKeyedArchiver to convert the object to data and store in userDefaults. Use NSKeyedUnarchiver to convert the data back to the object from defaults.

//h file
@interface Class : NSObject<NSCoding>

@property (nonatomic, copy) NSString *idClass;
@property (nonatomic, copy) NSString *nameClass;


//m File

- (void)encodeWithCoder:(NSCoder *)aCoder{

    [aCoder encodeObject:self.idClass forKey:@"IDClass"];
    [aCoder encodeObject:self.nameClass forKey:@"NameClass"];

}

- (id)initWithCoder:(NSCoder *)aDecoder{

    self = [super init];

    if (self) {
        self.idClass = [aDecoder decodeObjectForKey:@"IDClass"];
        self.nameClass = [aDecoder decodeObjectForKey:@"NameClass"];
    }

    return self;
}

After implementing the NSCoding you can archive them to store as data in userDefaults

//Saving array
Class *aClass = [[Class alloc]init];    
aClass.idClass=@"aaaxxx";    
aClass.nameClass=@"hello";

[myArray addObject:aClass];

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:myArray];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:data forKey:@"savedArray"];
[defaults synchronize]; 

//Retrieving 

NSData *data = [defaults objectForKey:@"savedArray"];
NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:data];


来源:https://stackoverflow.com/questions/16519443/nsmutablearray-not-saved-nsuserdefault

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!