iphone开发中的数据存储:archiving model Objects

简介:

让Model类遵循NSCoding 和 NSCopying 协议

并实现三个方法:

复制代码
- (void)encodeWithCoder:(NSCoder *)encoder {

/*
[super encodeWithCoder:encoder];
//If you are subclassing a class that also conforms to NSCoding, you need to make sure you call encodeWithCoder: on your superclass,
*/
[encoder encodeObject:foo forKey:kFooKey];
[encoder encodeObject:bar forKey:kBarKey];
[encoder encodeInt:someInt forKey:kSomeIntKey];
[encoder encodeFloat:someFloat forKey:kSomeFloatKey]
}
复制代码
复制代码
- (id)initWithCoder:(NSCoder *)decoder 
{
if (self = [super init]) {
//if(self = [super initWithCoder:decoder])
foo = [decoder decodeObjectForKey:kFooKey];
bar = [decoder decodeObjectForKey:kBarKey];
someInt = [decoder decodeIntForKey:kSomeIntKey];
someFloat = [decoder decodeFloatForKey:kAgeKey];

}
return self;
}
复制代码
复制代码
- (id)copyWithZone:(NSZone *)zone {
MyClass *copy = [[[self class] allocWithZone:zone] init];
copy.foo = [self.foo copyWithZone:zone];
copy.bar = [self.bar copyWithZone:zone];
copy.someInt = self.someInt;
copy.someFloat = self.someFloat;
return copy;
}
复制代码


在存储四个textField的字符的例子中,Model代码如下

复制代码
#pragma mark NSCoding
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:field1 forKey:kField1Key];
[encoder encodeObject:field2 forKey:kField2Key];
[encoder encodeObject:field3 forKey:kField3Key];
[encoder encodeObject:field4 forKey:kField4Key];
}

- (id)initWithCoder:(NSCoder *)decoder {
if (self = [super init]) {
field1 = [decoder decodeObjectForKey:kField1Key];
field2 = [decoder decodeObjectForKey:kField2Key];
field3 = [decoder decodeObjectForKey:kField3Key];
field4 = [decoder decodeObjectForKey:kField4Key];
}
return self;
}

#pragma mark -
#pragma mark NSCopying
- (id)copyWithZone:(NSZone *)zone {
BIDFourLines *copy = [[[self class] allocWithZone: zone] init];
copy.field1 = [self.field1 copyWithZone:zone];
copy.field2 = [self.field2 copyWithZone:zone];
copy.field3 = [self.field3 copyWithZone:zone];
copy.field4 = [self.field4 copyWithZone:zone];
return copy;
}
复制代码

依然在ViewController中定义两个方法:

- (NSString *)dataFilePath;

- (void)applicationWillResignActive:(NSNotification *)notification;

复制代码
#define kFilename         @"archive"
#define kDataKey @"Data"

- (NSString *)dataFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFilename];
}


- (void)applicationWillResignActive:(NSNotification *)notification {
BIDFourLines *fourLines = [[BIDFourLines alloc] init];
fourLines.field1 = field1.text;
fourLines.field2 = field2.text;
fourLines.field3 = field3.text;
fourLines.field4 = field4.text;

NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]
initForWritingWithMutableData:data];
[archiver encodeObject:fourLines forKey:kDataKey];
[archiver finishEncoding];
[data writeToFile:[self dataFilePath] atomically:YES];
}
复制代码


之后在viewDidLoad中实现:

复制代码
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSData *data = [[NSMutableData alloc]
initWithContentsOfFile:[self dataFilePath]];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]
initForReadingWithData:data];
BIDFourLines *fourLines = [unarchiver decodeObjectForKey:kDataKey];
[unarchiver finishDecoding];

field1.text = fourLines.field1;
field2.text = fourLines.field2;
field3.text = fourLines.field3;
field4.text = fourLines.field4;
}

UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillResignActive:)
name:UIApplicationWillResignActiveNotification
object:app];
}
复制代码



本文转自老Zhan博客园博客,原文链接:http://www.cnblogs.com/mybkn/archive/2012/03/27/2419054.html,如需转载请自行联系原作者

相关文章
|
1月前
|
编解码 测试技术 iOS开发
iPhone 屏幕尺寸和开发适配
【10月更文挑战第23天】iPhone 的屏幕尺寸变化给开发者带来了一定的挑战,但也为创新提供了机遇。通过深入了解不同屏幕尺寸的特点,遵循适配原则和策略,运用合适的技巧和方法,我们能够为用户提供在不同 iPhone 机型上都具有良好体验的应用。在未来,随着技术的不断进步,我们还需要持续学习和适应,以满足用户对优质应用体验的不断追求。
|
1月前
|
编解码 iOS开发 UED
响应式设计在 iPhone 开发适配中的具体应用
【10月更文挑战第23天】响应式设计在 iPhone 开发适配中扮演着至关重要的角色,它能够帮助我们打造出适应不同屏幕尺寸和用户需求的高质量应用。通过合理运用响应式设计的原则和方法,我们可以在提供良好用户体验的同时,提高开发效率和应用的可维护性。
|
4月前
|
数据采集 iOS开发 Python
Chatgpt教你开发iPhone风格计算器,Python代码实现
Chatgpt教你开发iPhone风格计算器,Python代码实现
|
Shell iOS开发
iOS逆向:tweak开发教程(iPhone/tool)
iOS逆向:tweak开发教程(iPhone/tool)
1231 0
iOS逆向:tweak开发教程(iPhone/tool)
|
编解码 iOS开发
iphone 开发的基本入门知识
iphone 开发的基本入门知识
235 0
「镁客早报」iPhone或将在今年采用三摄;传Facebook致力于开发语音助力服务与亚马逊、苹果竞争
亚马逊向美国Alexa设备推免费音乐服务;视频会议软件开发商Zoom纳斯达克上市。
269 0
|
Web App开发 缓存 开发工具
|
存储 iOS开发 计算机视觉
|
Web App开发 前端开发 JavaScript