在使用NSUserDefaults的时候插入数据有时候会报以下错误:Attempt to set a non-property-list objec

这种错误的原因是插入了不识别的数据类型,NSUserDefaults支持的数据类型有NSString、 NSNumber、NSDate、 NSArray、NSDictionary、BOOL、NSInteger、NSFloat等系统定义的数据类型。

解决办法把数组归档之后再进行存储直接上代码

模型数据的代码

#import <Foundation/Foundation.h>

@interface itemModel : NSObject
@property(nonatomic,copy)NSString* name;
@property(nonatomic,copy)NSString* number; - (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)appInfoWithDict:(NSDictionary *)dict;
+ (NSArray *)appinfoArrayWithArray:(NSArray *)array; @end
#import "itemModel.h"
@interface itemModel () @end
@implementation itemModel - (instancetype)initWithDict:(NSDictionary *)dict
{
self = [super init];
if (self) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
} + (instancetype)appInfoWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{ }
+ (NSArray *)appinfoArrayWithArray:(NSArray *)arr
{
NSMutableArray *arrayM = [NSMutableArray array]; for (NSDictionary *dict in arr) {
[arrayM addObject:[self appInfoWithDict:dict]];
}
return arrayM;
} -(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:_name forKey:@"name"]; [aCoder encodeObject:_number forKey:@"number"];
} - (id)initWithCoder:(NSCoder*)aDecoder { if (self = [super init]) { _name = [aDecoder decodeObjectForKey:@"name"];
_number = [aDecoder decodeObjectForKey:@"number"];
} return self; }
@end
使用方法
//解档获得数组
NSData * data= [[NSUserDefaults standardUserDefaults]objectForKey:@"key"];
NSArray * appArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSMutableArray * mut = [NSMutableArray arrayWithArray:appArray];
[mut addObject:mode];
//归档存储数组
NSData *data1 = [NSKeyedArchiver archivedDataWithRootObject:[mut mutableCopy]];
[userDefaults setObject:data1 forKey:@"key"];
[userDefaults synchronize];

NSUserDefaults保存对象数组报错的更多相关文章

  1. java使用类数组 报错Exception in thread "main" java.lang.NullPointerException

    源代码如下: Point[] points=new Point[n];//Point是一个类 for(int i=0;i<n;i++) { System.out.print("请输入x ...

  2. mybatis问题。foreach循环遍历数组报错情况,及其解决方法

    根据条件查询数据列表,mybatis查询代码如下 如果只查询属于特定部门拥有的数据权限.这需要用 String[ ] codes保存当前部门及其子部门的部门编码. 所以需要在mybatis中遍历编码数 ...

  3. 解决spring-boot配置文件使用加密方式保存敏感数据启动报错No decryption for FailsafeTextEncryptor. Did you configure the keystore correctly

    spring-boot配置文件使用加密方式保存敏感数据 application.yml spring: datasource: username: dbuser password: '{cipher} ...

  4. Cookie保存中文用户名报错(500)

    在用Cookie保存用户名时候,当用户名是中文的时候服务器报错了. HTTP Status 500 - An exception occurred processing JSP page /dolog ...

  5. Tuple解决在视图中通过razor获取控制器传递给视图的匿名对象的报错问题

    C#的编译器总是将匿名类型编译成internal的,当在视图中直接使用控制器传递的匿名对象时就会报错错误代码:控制器代码视图代码执行结果: ****************************** ...

  6. 解决tensorflow模型保存时Saver报错:TypeError: TF_SessionRun_wrapper: expected all values in input dict to be ndarray

    TypeError: TF_SessionRun_wrapper: expected all values in input dict to be ndarray 对于下面的实际代码: import ...

  7. nuxtjs在vue组件中使用window对象编译报错的解决方法

    我们知道nuxtjs是做服务端渲染的,他有很多声明周期是运行在服务端的,以及正常的vue声明周期mounted之前均是在服务端运行的,那么服务端是没有比如window对象的location.navag ...

  8. win下python脚本以unix风格换行保存将会报错为编码问题 SyntaxError: encoding problem:gbk

    utf-8与gbk编码都报错 从别人的github拉下来一个python脚本. 直接运行,python报错如下: File ".\drag_files_do_event.py", ...

  9. PostgreSQL 大对象导出报错问题分析

    1.前言 在处理用户问题过程遇到一个问题.用户通过pg_dump导出 bytea 对象时,当行的大小超过 1G时,会报错: [v8r6c5b41@dbhost01 ~]$ sys_dump -t t1 ...

随机推荐

  1. [ucos]了解ucos

    1. uCosIII移植到STM32F10x http://www.cnblogs.com/hiker-blogs/archive/2012/06/13/2547176.html 2. uCosIII ...

  2. C#调用SQL Server有参的存储过程

    一.使用SqlParameter的方式 代码: using System; using System.Collections.Generic; using System.ComponentModel; ...

  3. Streams:深入剖析Redis5.0全新数据结构

    Streams:深入剖析Redis5.0全新数据结构   原创: 阿飞的博客   Redis 5.0 全新的数据类型:streams,官方把它定义为:以更抽象的方式建模日志的数据结构.Redis的st ...

  4. JQuery Show()的几种效果 总有一种是你需要的

    1 show()方法和hide()方法 $("selector").show()  从display:none还原元素默认或已设置的display属性$("selecto ...

  5. doAfterBody()方法是在( )接口中定义的。

    A.Tag B.IterationTag C.BodyTag D.TagSupport 解答:B

  6. (转)FS_S5PC100平台上Linux Camera驱动开发详解(二)

    4-3 摄像头的初始化流程及v4l2子设备驱动 这个问题弄清楚了以后下面就来看获得Camera信息以后如何做后续的处理: 在fimc_init_global调用结束之后我们获得了OV9650的信息,之 ...

  7. 【BZOJ】1673: [Usaco2005 Dec]Scales 天平(dfs背包)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1673 bzoj翻译过来的c<=230不忍吐槽......................... ...

  8. ssh免密码登录的几个注意事项

    1, authorized_keys文件中每个公钥占一行,不能分成多行. 2,文件夹默认权限为600 3,如果遇到奇怪的问题,可以把.ssh/文件全部删掉,重新用ssh-keygen生成.

  9. powershell---高级函数的介绍

    https://guhuajun.wordpress.com/2009/05/11/windows-powershell-v2-介绍(5)-高级函数(上)/ https://guhuajun.word ...

  10. 复习及总结--.Net线程篇(3)

    不幸的发现,原来多线程的东西还有好多. 不只是一个Thread就能把事情做完的,好吧,孤陋寡闻了 这里总结下  复习及总结--.Net线程篇(2)里的两个概念AppDomain和ThreadPool ...