iOS NSUserDefaults 存儲可變數組問題

前言

NSUserDefaults支持的數據類型有:NSNumber(NSInteger、float、double),NSString,NSDate,NSArray,NSDictionary,BOOL.數組

附:NSUserDefaults 雖然自己不支持自定義對象的存儲,不過它支持NSData的類型。因此當咱們要在NSUserDefaults中存儲的是自定義的對象的時候,須要將該自定義對象轉成NSData存儲。而自定義對象轉data的方式咱們經過<NSCoding>來實現。這裏提早講到,下面會詳細介紹。ui

一、NSUserDefaults 存儲數組問題

①若是數組中的對象不是自定義的對象,那麼可直接存儲。如:編碼

NSArray *array = @[@"1", @"2", @"3", @"4"]; [[NSUserDefaults standardUserDefaults] setObject:array forKey:@"key"]; [[NSUserDefaults standardUserDefaults] synchronize]; 

②若是數組中的對象是自定義的對象,那麼須要先讓這個自定義類實現<NSCoding>協議中的- (id) initWithCoder: (NSCoder *)coder方法和- (void) encodeWithCoder: (NSCoder *)coder方法,而後把該自定義的類對象編碼到NSData中,再從NSUserDefaults中進行讀取。atom

//User.h
@interface User : NSObject<NSCoding> //注意:這裏須要實現NSCoding協議

@property (nonatomic, copy) NSString *realName;
@property (nonatomic, copy) NSString *nickName;
@property (nonatomic, copy) NSString *password;

@end

//User.m
@implementation User

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super init]) {
        self.realName = [aDecoder decodeObjectForKey:@"realName"];
        self.nickName = [aDecoder decodeObjectForKey:@"nickName"];
        self.password = [aDecoder decodeObjectForKey:@"password"];
    }
    
    return self;
}


- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:self.realName forKey:@"realName"];
    [aCoder encodeObject:self.nickName forKey:@"nickName"];
    [aCoder encodeObject:self.password forKey:@"password"];
}

@end
這時,存儲自定義對象的數組的獲取與保存方法以下

    //存儲
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSArray *array = @[customObject1, customObject2, customObject3, customObject4];
    NSData *arrayData = [NSKeyedArchiver archivedDataWithRootObject:array];
    [userDefaults setObject:arrayData forKey:@"arrayKey"];
    [userDefaults synchronize];
    
    //取出
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSData *arrayData = [userDefaults  objectForKey:@"arrayKey"];
    NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:arrayData];

 

二、NSUserDefaults 存儲自定義對象的問題見上
三、修改存儲在NSUserDefaults中的數組

廢話很少說上代碼,項目中遇到的問題是:spa

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *array = [userDefaults objectForKey:@"theArrayKey"]];

[array addObject:@"some new value"];
[userDefaults setObject: array forKey:@"theArrayKey"]; //會卡住主程序

[[NSUserDefaults standardUserDefaults] synchronize];

 

這句話取出了數組,但是當對數組添加元素後,進行存儲時卡在了下面這句話:
[userDefaults setObject: array forKey:@"theArrayKey"];
時候會把主線程卡住,可是不崩潰,不知道爲什麼,就Google了,stackoverflow給瞭解答辦法,.net

When you store mutable objects to NSUserDefaults, it stores an immutable copy of it so you can't change it directly like that. You have to get the mutable copy out of defaults, change it, and then set it back, replacing old object in defaults.
即:修改存儲在NSUserDefaults中的數組:咱們從NSUserDefaults中取出的數組是不可變的(由於NSUserDefaults 存儲的對象全是不可變的)。因此當咱們須要修改存儲在NSUserDefaults中的數組時,須要用一個新的可變數組來保存以前的值,再修改,以後再保存,即修改的過程應該以下:線程

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *array = [userDefaults objectForKey:@"theArrayKey"]];

NSMutableArray *mutableCopyArray = [array mutableCopy]; //重要步驟操做mutableCopyArray
[mutableCopyArray addObject:@"some new value"];
[userDefaults setObject: mutableCopyArray forKey:@"theArrayKey"];

[[NSUserDefaults standardUserDefaults] synchronize];

 

總結:NSUserDefaults 存儲的對象全是不可變的(這一點很是關鍵,弄錯的話程序會出bug),即存進NSUserDefaults的對象會變成不可變的,一樣取出來的對象確定是不可變的。code

其它

參考:NSUserDefaults 簡介,使用 NSUserDefaults 存儲自定義對象對象

相關文章
相關標籤/搜索