【深入浅出ios开发】存储一般的数据对象
2015-03-30 14:49
417 查看
一般通过NSKeyedArchiver来存储一般的对象:
重点在encode和decode
存储数据:
[objc] view
plaincopy
- (IBAction)save:(id)sender {
MrPerson *person = [[MrPerson alloc]init];
person.name = @"Ranran";
person.age = 16;
person.height = 1.62;
[NSKeyedArchiver archiveRootObject:person toFile:@"/Users/misaka/Desktop/0410/p.Data"];
}
读取数据:
[objc] view
plaincopy
- (IBAction)read:(id)sender {
MrPerson *person = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/misaka/Desktop/0410/p.Data"];
NSLog(@"%@---%ld-----%f",person.name,(long)person.age,person.height);
}
Person类头文件中的设置:
[objc] view
plaincopy
#import <Foundation/Foundation.h>
@interface MrPerson : NSObject<NSCoding>
@property (nonatomic,copy) NSString* name;
@property (nonatomic,assign) NSInteger age;
@property (nonatomic,assign) double height;
@end
Person类实现文件中的设置:
[objc] view
plaincopy
#import "MrPerson.h"
@implementation MrPerson
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInteger:self.age forKey:@"age"];
[aCoder encodeDouble:self.height forKey:@"height"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super init]) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeIntegerForKey:@"age"];
self.height = [aDecoder decodeDoubleForKey:@"height"];
}
return self;
}
@end
重点在encode和decode
存储数据:
[objc] view
plaincopy
- (IBAction)save:(id)sender {
MrPerson *person = [[MrPerson alloc]init];
person.name = @"Ranran";
person.age = 16;
person.height = 1.62;
[NSKeyedArchiver archiveRootObject:person toFile:@"/Users/misaka/Desktop/0410/p.Data"];
}
读取数据:
[objc] view
plaincopy
- (IBAction)read:(id)sender {
MrPerson *person = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/misaka/Desktop/0410/p.Data"];
NSLog(@"%@---%ld-----%f",person.name,(long)person.age,person.height);
}
Person类头文件中的设置:
[objc] view
plaincopy
#import <Foundation/Foundation.h>
@interface MrPerson : NSObject<NSCoding>
@property (nonatomic,copy) NSString* name;
@property (nonatomic,assign) NSInteger age;
@property (nonatomic,assign) double height;
@end
Person类实现文件中的设置:
[objc] view
plaincopy
#import "MrPerson.h"
@implementation MrPerson
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInteger:self.age forKey:@"age"];
[aCoder encodeDouble:self.height forKey:@"height"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super init]) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeIntegerForKey:@"age"];
self.height = [aDecoder decodeDoubleForKey:@"height"];
}
return self;
}
@end
相关文章推荐
- 【深入浅出ios开发】存储一般的数据对象
- iOS开发-进阶:数据存储之-对模型对象归档(NSKeyedArchiver、NSKeyedUnarchiver)
- 【深入浅出ios开发】数据存储-----XML属性列表(plist)归档
- ios-day12-04(ios开发中数据存储之NSKeyedArchiver归档(NSCoding)、普通对象如何归档、普通对象的子类如何归档)
- 【iOS开发-75】iOS数据存储的三种简单方式:plist、preference以及用NSCoding存储对象
- 【深入浅出ios开发】数据存储-----XML属性列表(plist)归档
- [转] iOS开发24:使用SQLite3存储和读取数据
- iOS开发:使用SQLite3存储和读取数据
- 【iOS-cocos2d-X 游戏开发之六】使用Base64算法对Cocos2dX自带CCUserDefault游戏存储数据编码!
- 【iOS-Cocos2d游戏开发之十九】游戏数据存储的四种常用方式NSKeyedArchiver/NSUserDefaults/Write写入/SQLite3
- 在ios开发中遇到数据最好采用c语言中的数组存储
- ios开发 数据存储
- ios开发:NSKeyedUnarchiver,数据序列化,本地存储
- iOS开发24:使用SQLite3存储和读取数据
- iOS开发22:通过归档永久存储数据
- ios开发 数据存储
- IOS iPhone 开发中的文件读写及数据存储(一)
- 【iOS-Cocos2d游戏开发之十九】游戏数据存储的四种常用方式NSKeyedArchiver/NSUserDefaults/Write写入/SQLite3
- 【iOS-cocos2d-X 游戏开发之六】使用Base64算法对Cocos2dX自带CCUserDefault游戏存储数据加密!
- 【iOS-Cocos2d游戏开发之十九】游戏数据存储的四种常用方式NSKeyedArchiver/NSUserDefaults/Write写入/SQLite3