iOS-NSUserDefault 提供简单的key-value存储

//存储

- (void)_saveListDataWithArray:(NSArray *)modelArray {

    //序列化

    NSData *listData = [NSKeyedArchiver archivedDataWithRootObject:modelArray requiringSecureCoding:YES error:nil];

    [[NSUserDefaults standardUserDefaults] setObject:listData forKey:@"listDataKey"];

    [[NSUserDefaults standardUserDefaults] synchronize];

}

//读取

- (NSArray *)_readListDataFromLocal {

    NSData *readListData = [[NSUserDefaults standardUserDefaults] objectForKey:@"listDataKey"];

    ///反序列化

    id unarchiveObj = [NSKeyedUnarchiver unarchivedObjectOfClasses:[NSSet setWithObjects:[NSArray class], [HomeListModel class], nil] fromData:readListData error:nil];

    if([unarchiveObj isKindOfClass:[NSArray class]] && [unarchiveObj count] >0) {

        return(NSArray *)unarchiveObj;

    }

    return nil;

}



你可能感兴趣的:(iOS-NSUserDefault 提供简单的key-value存储)