需要编写对象必须实现NSCoding protocol

Person Class

Person.h

#import <Foundation/Foundation.h>

#import "Face.h"

@interface Person : NSObject <NSCoding>

@property (nonatomic, strong) NSString      *personId;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, strong) NSArray *photos;
@property (nonatomic, strong) NSDictionary *phoneNumber; @property (nonatomic, strong) Face *face; @end Person.m #import "Person.h" @implementation Person
@synthesize personId = _personId;
@synthesize name = _name;
@synthesize age = _age;
@synthesize photos = _photos;
@synthesize phoneNumber = _phoneNumber;
@synthesize face = _face; - (id)initWithCoder:(NSCoder *)aDecoder
{
if(self = [super init])
{
self.personId = [aDecoder decodeObjectForKey:@"id"];
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [[aDecoder decodeObjectForKey:@"age"] integerValue];
self.photos = [aDecoder decodeObjectForKey:@"photos"];
self.phoneNumber = [aDecoder decodeObjectForKey:@"phoneNumber"];
self.face = [aDecoder decodeObjectForKey:@"face"];
}
return self;
} - (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.personId forKey:@"id"];
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:[NSNumber numberWithInteger:self.age] forKey:@"age"];
[aCoder encodeObject:self.photos forKey:@"photos"];
[aCoder encodeObject:self.phoneNumber forKey:@"phoneNumber"];
[aCoder encodeObject:self.face forKey:@"face"];
}

Face Class

Face.h

#import <Foundation/Foundation.h>

@interface Face : NSObject <NSCoding>

@property (nonatomic, strong) NSString *head;
//@property (nonatomic, strong) NSString *eyes;
//@property (nonatomic, strong) NSString *nose;
//@property (nonatomic, strong) NSString *mouth;
//@property (nonatomic, strong) NSString *ears; @end Face.m #import "Face.h" @implementation Face - (id)initWithCoder:(NSCoder *)aDecoder
{
if(self = [super init])
{
self.head = [aDecoder decodeObjectForKey:@"head"];
}
return self;
} - (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.head forKey:@"head"];
} @end
写入和读取操作

    Person *person = [[Person alloc] init];
person.personId = @"123456789";
person.name = @"Hunk";
person.age = 10;
person.photos = @[@"a.png", @"b.png", @"c.png"];
person.phoneNumber = @{@"mobile_phone" : @"987654321", @"work" : @"01012345678"};
Face *face = [[Face alloc] init];
face.head = @"Round shape";
person.face = face; NSData *personData0 = [NSKeyedArchiver archivedDataWithRootObject:person]; [[NSUserDefaults standardUserDefaults] setObject:personData0 forKey:@"person"];
[[NSUserDefaults standardUserDefaults] synchronize]; NSData *personData1 = [[NSUserDefaults standardUserDefaults] objectForKey:@"person"];
Person *person1 = [NSKeyedUnarchiver unarchiveObjectWithData:personData1];

版权声明:本文博主原创文章,博客,未经同意不得转载。

NSUserDefaults写作和阅读对象定义自己的更多相关文章

  1. openerp经典收藏 对象定义详解(转载)

    对象定义详解 原文地址:http://shine-it.net/index.php/topic,2159.0.htmlhttp://blog.sina.com.cn/s/blog_57ded94e01 ...

  2. sql server查询可编程对象定义的方式对比以及整合

    本文目录列表: 1.sql server查看可编程对象定义的方式对比 2.整合实现所有可编程对象定义的查看功能的存储dbo.usp_helptext2 3.dbo.helptext2的选择性测试 4. ...

  3. js对象定义

    JS中的对象定义方式,跟服务端,还是有很大差别的! 现在来说一下JS类的定义 工厂模式 function creatHeven(name,age){ var temp =new Object(); t ...

  4. 使用prototype 对象定义类成员

    使用prototype 对象定义类成员上一节介绍了类的实现机制以及构造函数的实现,现在介绍另一种为类添加成员的机制:prototype 对象.当new 一个function 时,该对象的成员将自动赋给 ...

  5. javascript对象定义和操作

    //js对象定义有三种方式//js方法定义有三种方式 function fn(){} var fun = function(){} var fun = new function() {} //**** ...

  6. asp.net MVC中使用Autofac小结 (遇到的最傻错误: 没有为该对象定义无参数的构造函数)

    项目使用的MVC4,.net 4.5 Nuget安装最新的autofac,一直提示不支持.net 4.5.没办法了,最后用Nuget控制台安装的老版本.因为我使用的是MVC4,所以直接安装的是auto ...

  7. .Net Framework 4.5.1 ASP.NET MVC 5 下新建视图报“错误 运行所选代码生成器时出错 无法检索元数据 没有为该对象定义无参数构造函数”

    当在控制器中新建视图的时候,选择的视图界面如下: 执行添加后报如下错误: 错误的内容为: 错误运行所选代码生成器时出错 无法检索"XXX"的元数据没有为该对象定义无参数构造函数 U ...

  8. SQL Server中授予用户查看对象定义的权限

    SQL Server中授予用户查看对象定义的权限   在SQL Server中,有时候需要给一些登录名(用户)授予查看所有或部分对象(存储过程.函数.视图.表)的定义权限存.如果是部分存储过程.函数. ...

  9. kbmmw ORM 对象定义语法简析

    使用kbmmw 的ORM 一定先要了解ORM 的对象定义语法. 下面简单说一下 // kbmMW_Table - Define a table. 定义一个表 // Must be used on cl ...

随机推荐

  1. ERWin 7.1 和7.2 的官方FTP下载地址

    ERWin 7.1 下载地址: ftp://ftp.ca.com/CAproducts/erwin/ServicePacks/AFEDM71sp2-b1303.exe ERWin 7.2 下载地址: ...

  2. 与众不同 windows phone (20) - Device(设备)之位置服务(GPS 定位), FM 收音机, 麦克风, 震动器

    原文:与众不同 windows phone (20) - Device(设备)之位置服务(GPS 定位), FM 收音机, 麦克风, 震动器 [索引页][源码下载] 与众不同 windows phon ...

  3. jquery中怎么删除<ul>中的整个<li>包括节点

    .$('ul li').remove(); .$('ul li').each(function(){ $(this).remove(); }); .$("ul").find(&qu ...

  4. SilkTest Q&A 6

    Q51.GMO在线的问题? 该问题是一个特例,不具有代表性,故不翻译了. Q52.如何为一个testplan的属性定义值? A52:你必须在使用前为一个testplan定义值: 1.确保你的test ...

  5. cannot load supported formats intellij 解决的方法

    http://stackoverflow.com/questions/20797443/intellij-idea-subversion-checkout-error http://stackover ...

  6. 论javascript模块化的优缺

    如今backbone.emberjs.spinejs.batmanjs 等MVC框架侵袭而来.CommonJS.AMD.NodeJS.RequireJS.SeaJS.curljs等模块化的JavaSc ...

  7. qt qml中PropertyAnimation的几种使用方法

    qml文章 qt qml中PropertyAnimation的几种使用方法 动画应用场景有以下几种: 首先如果一个Rectangle.动画是要改变它的x和y值 1,Rectangle一旦被创建,就要移 ...

  8. What is tradebit?

    The Tradebit Fact Sheet What is tradebit?

  9. dell服务器从硬盘导入阵列信息

    前几天去南京客户那里更新新的业务系统,客户要求将服务器上的旧的硬盘拆下来,换上新的硬盘,重新做raid,客户自己要插入旧的硬盘读取旧数据,昨天做了几个实验,两台Dell R710服务器各4块硬盘,一台 ...

  10. Ubuntu 使用中的问题总结

    1.ibus输入法图标消失 #ibus-daemon -d 2.Ubuntu没有声音 #alsamixer 3.“对不起,ubuntu出现了内部错误” # nano /etc/default/appo ...