NSMutableArray未保存NSUserDefault

时间:2013-05-13 10:05:20

标签: objective-c xcode nsmutablearray nsuserdefaults

我正在尝试使用NSUSerDefault保存NSMutableArray,然后打开数组。 这里有一些代码:

-(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]; 
}

- (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.
}

当我编译和按下按钮时,这是我在日志输出中读到的内容:

[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

显然当我重新打开视图时,数组未加载。 有什么帮助吗?

3 个答案:

答案 0 :(得分:3)

NSUserDefaults仅允许将原始数据类型存储在其中。如果要存储自定义类Object,请使用以下代码,有关详细信息,请参阅此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]);

答案 1 :(得分:0)

您需要在模型对象Class中实现NSCoding协议。需要将非标准数据序列化和反序列化为NSUserDefaults

现在,您可以使用NSKeyedArchiver将对象转换为数据并存储在userDefaults中。使用NSKeyedUnarchiver将数据从默认值转换回对象。

//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;
}

实施NSCoding后,您可以将它们存档以存储为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];

答案 2 :(得分:0)

首先在对象类中实现NSCoding。为了保存/加载你可以做的那些对象的NSMutableArray:

- (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];
}

有关更多信息,我建议您查看更长的教程。例如Ray Wenderlich的How to Save your App Data with NSCoding And NSFileManager是一个很好的。