iphone开发中的数据存储:Property lists

简介:

定义两个方法:

- (NSString *)dataFilePath;获取返回数据文件的完整路径名

- (void)applicationWillResignActive:(NSNotification *)notification;保存数据,参数notification的作用是在对象之间传递通知,保持通信。

复制代码
#define kFilename        @"data.plist"


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

 

复制代码
- (void)applicationWillResignActive:(NSNotification *)notification {
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:field1.text];
[array addObject:field2.text];
[array addObject:field3.text];
[array addObject:field4.text];
[array writeToFile:[self dataFilePath] atomically:YES];
}
复制代码

(例子程序为保存四个textField的字符)

之后在viewDidLoad中添加代码:

复制代码
- (void)viewDidLoad
{
[super viewDidLoad];

NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
field1.text = [array objectAtIndex:0];
field2.text = [array objectAtIndex:1];
field3.text = [array objectAtIndex:2];
field4.text = [array objectAtIndex:3];
}

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/2418996.html,如需转载请自行联系原作者

相关文章
|
编解码 iOS开发
iphone 开发的基本入门知识
iphone 开发的基本入门知识
147 0
「镁客早报」iPhone或将在今年采用三摄;传Facebook致力于开发语音助力服务与亚马逊、苹果竞争
亚马逊向美国Alexa设备推免费音乐服务;视频会议软件开发商Zoom纳斯达克上市。
225 0
|
Web App开发 缓存 开发工具
|
存储 iOS开发 计算机视觉
|
Web App开发 前端开发 JavaScript
|
API iOS开发 编解码
iOS开发UI篇—iPad和iPhone开发的比较
iOS开发UI篇—iPad和iPhone开发的比较 一、iPad简介 1.什么是iPad   一款苹果公司于2010年发布的平板电脑   定位介于苹果的智能手机iPhone和笔记本电脑产品之间   跟iPhone一样,搭载的是iOS操作系统    2.
1003 0