场景是这样的:

现在有一个数组如下,数组中存放着自定义的对象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。

最新文章

  1. Restful api介绍
  2. c++强制类型转换(static_cast,const_cast,dynamic_cast,reinterpret_cast)
  3. shell 基本结构
  4. 7 天玩转 ASP.NET MVC — 第 7 天
  5. 直接下载完整chrome浏览器的方法
  6. WinCE发展史
  7. MapReduce概述,原理,执行过程
  8. mysql优化案例
  9. z-index的理解 z-index 属性仅在节点的 position 属性为 relative, absolute 或者 fixed 时生效.
  10. Win7下VS2010使用STLPort 和boost1.56.
  11. [Spring入门学习笔记][Spring Boot]
  12. java学习——数组
  13. 控制流之continue
  14. JavaWeb之MVC模式
  15. JAVA中JPA的主键自增长注解设置
  16. R语言-逻辑回归建模
  17. mysql5.7.17版本升级源码方式及恢复主主复制
  18. sublime构建各个编译环境
  19. 重装windows系统后配置Anaconda
  20. 基于TCP/IP协议的socket通讯server

热门文章

  1. event级别设置Resumable Space Allocation
  2. CSS3/jQuery自己定义弹出窗体
  3. SQL Server XML Path[转]
  4. UNIX基础知识之信号
  5. LeetCode: Binary Tree Traversal
  6. How to Setup Replicated LevelDB Persistence in Apache ActiveMQ 5.9--转载
  7. SQLServer 之 常用函数及查看
  8. 英文破折号(em dash)、连接号(en dash)与连字符(hyphen)的区别及各自用法是什么?
  9. 数据结构笔记02:Java面试必问算法题
  10. html笔记03:表单