NSUserDefaults存储自定义对象

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NSUserDefaults存储自定义对象相关的知识,希望对你有一定的参考价值。

场景是这样的:

现在有一个数组如下,数组中存放着自定义的对象GoodscCategory

<__NSArrayM 0x7ffb9c2032b0>(
<GoodscCategory: 0x7ffb9c2079f0>,
<GoodscCategory: 0x7ffb9c2229e0>,
<GoodscCategory: 0x7ffb9c2217a0>,
<GoodscCategory: 0x7ffb9c222c30>,
<GoodscCategory: 0x7ffb9c21d710>,
<GoodscCategory: 0x7ffb9c21afe0>,
<GoodscCategory: 0x7ffb9c223ff0>,
<GoodscCategory: 0x7ffb9c221f80>,
<GoodscCategory: 0x7ffb9c21fcf0>,
<GoodscCategory: 0x7ffb9c224bf0>,
<GoodscCategory: 0x7ffb9c224c10>,
<GoodscCategory: 0x7ffb9c21a0e0>,
<GoodscCategory: 0x7ffb9c0a0550>
)

在尝试将该数组存储在NSUserDefaults时,发生了如下错误:

Attempt to set a non-property-list object (
    "<GoodscCategory: 0x7ffb9c2079f0>",
    "<GoodscCategory: 0x7ffb9c2229e0>",
    "<GoodscCategory: 0x7ffb9c2217a0>",
    "<GoodscCategory: 0x7ffb9c222c30>",
    "<GoodscCategory: 0x7ffb9c21d710>",
    "<GoodscCategory: 0x7ffb9c21afe0>",
    "<GoodscCategory: 0x7ffb9c223ff0>",
    "<GoodscCategory: 0x7ffb9c221f80>",
    "<GoodscCategory: 0x7ffb9c21fcf0>",
    "<GoodscCategory: 0x7ffb9c224bf0>",
    "<GoodscCategory: 0x7ffb9c224c10>",
    "<GoodscCategory: 0x7ffb9c21a0e0>",
    "<GoodscCategory: 0x7ffb9c0a0550>"
) as an NSUserDefaults/CFPreferences value for key sortDataArray

经过查询,发现原因是:

NSUserDefaults支持的数据格式有:NSNumber(Integer、Float、Double),NSString,NSDate,NSArray,NSDictionary,BOOL类型,

如果想要保存其他类型或者自定义类型需要用到archiver将数据序列化为NSData类型,需要在自定义类中写encode和decode两个方法。

GoodscCategory.h

#import <Foundation/Foundation.h>

@interface GoodscCategory : NSObject

@property (nonatomic,copy) NSString *categoryID;
@property (nonatomic,copy) NSString *categoryName;
@property (nonatomic,retain) NSArray *subCategoryList;

@end

GoodscCategory.m

@implementation GoodscCategory

- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:_categoryID forKey:@"id"];
    [aCoder encodeObject:_categoryName forKey:@"name"];
    [aCoder encodeObject:_subCategoryList forKey:@"list"];
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super init]) {
        self.categoryID = [aDecoder decodeObjectForKey:@"id"];
        self.categoryName = [aDecoder decodeObjectForKey:@"name"];
        self.subCategoryList = [aDecoder decodeObjectForKey:@"list"];
    }
    return self;
}

@end

然后在存储的时候进行序列化

- (void)saveSortArrayData:(NSArray *)array {
    
    NSMutableArray *archiveArray = [NSMutableArray arrayWithCapacity:array.count];
    for (GoodscCategory *goodsObject in array) {
        NSData *goodsEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:goodsObject];
        [archiveArray addObject:goodsEncodedObject];
    }
    
    NSUserDefaults *userData = [NSUserDefaults standardUserDefaults];
    [userData setObject:archiveArray forKey:@"sortDataArray"];
}

取出的时候反序列化

NSArray * dataArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"sortDataArray"];
    NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
    for (NSData *goodsData in dataArray)
    {
        GoodscCategory  *goods = [NSKeyedUnarchiver unarchiveObjectWithData:goodsData];
        [mutableArray addObject:goods];
    }

这样,就实现了将数组array存入,使用的时候取出为数组mutableArray。

 

以上是关于NSUserDefaults存储自定义对象的主要内容,如果未能解决你的问题,请参考以下文章

NSUserDefaults存储自定义对象

将自定义对象存储到 NSMutableArray,然后将此 NSMutableArray 存储到 NSUserDefaults

NSUserDefaults 不在 iOS 8 中的应用程序和自定义键盘之间存储值

NSUserDefaults 简介

NSUserDefaults 保存自己定义对象

如何使用 NSUserDefaults 在 Swift 中存储自定义类数组?