iPhone开发数据持久化总结之第2篇—属性文件(.plist) .

本文介绍了一个简单的iOS应用案例,展示了如何使用属性列表文件(.plist)来实现数据的持久化存储。具体步骤包括创建项目、设计用户界面、编写代码以读写数据,并详细解释了数据的保存位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现的功能:1)演示使用属性文件持久化数据。

关键词:数据持久化 属性文件 plist


1、新建一个Sigle View Application,命名为Persistence-file,工程结构如下

[img]
[img]http://dl.iteye.com/upload/attachment/0078/7419/3a9fb508-d179-3c27-9bea-6d86d22f3818.png[/img]
[/img]


2、修改ViewController.xib,添加4个Label控件和4个TextField控件,如下:

[img]
[img]http://dl.iteye.com/upload/attachment/0078/7421/bf642f94-20dd-3752-8ea3-e1d7d3b748e1.png[/img]
[/img]


3、修改ViewController.h,如下:

#define kFileName @"data.plist"
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property(nonatomic,retain)IBOutlet UITextField *name;
@property(nonatomic,retain)IBOutlet UITextField *gender;
@property(nonatomic,retain)IBOutlet UITextField *age;
@property(nonatomic,retain)IBOutlet UITextField *education;

-(NSString *)dataFilePath;
-(void)applicationWillResignActive:(NSNotification *)nofication;

@end

注意,需要连接各个输出口


4、修改ViewController.m,如下:
#import "ViewController.h"

@implementation ViewController
@synthesize name,gender,age,education;

#pragma mark - View lifecycle
- (void)viewDidLoad
{
// Do any additional setup after loading the view, typically from a nib.
[super viewDidLoad];
UIApplication *app = [UIApplication sharedApplication];
//订阅通知UIApplicationWillResignActiveNotification,进行数据保存操作
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];
//初始化数据
[self initData];
}

-(void)initData{
NSString *filePath = [self dataFilePath];
NSLog(@"filePath=%@",filePath);

//从文件中读取数据,首先判断文件是否存在
if([[NSFileManager defaultManager] fileExistsAtPath:filePath]){
NSArray *array = [[NSArray alloc]initWithContentsOfFile:filePath];
name.text = [array objectAtIndex:0];
gender.text = [array objectAtIndex:1];
age.text = [array objectAtIndex:2];
education.text = [array objectAtIndex:3];

[array release];
}
}

-(void)applicationWillResignActive:(NSNotification *)nofication{
NSMutableArray *array = [[NSMutableArray alloc]init];
[array addObject:name.text];
[array addObject:gender.text];
[array addObject:age.text];
[array addObject:education.text];
//将数据写入到文件dataFilePath中
[array writeToFile:[self dataFilePath] atomically:YES];
[array release];
}

//获得文件路径
-(NSString *)dataFilePath{
//检索Documents目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);//备注1
NSString *documentsDirectory = [paths objectAtIndex:0];//备注2
return [documentsDirectory stringByAppendingPathComponent:kFileName];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.name = nil;
self.gender = nil;
self.age = nil;
self.education = nil;
}

-(void)dealloc{
[name release];
[gender release];
[age release];
[education release];
}

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end


备注1、备注2:
NSDocumentDirectory:常量,表明正在查找Documents目录的路径
NSUserDomainMask:常量,表面将搜索限制与当前应用程序的沙盒中,这样的话只能找到一个Documents目录,因为每个应用程序沙盒中只有一个Documents目录,所以备注2中从paths中取第一个即为当前应用程序的Documents目录的路径


5、编译、运行,在TextField中输入内容,然后退出Simulator,进行测试:

[img]
[img]http://dl.iteye.com/upload/attachment/0078/7423/a97c5739-4ece-3f7d-802a-ce6b070bd92c.png[/img]
[/img] [img]
[img]http://dl.iteye.com/upload/attachment/0078/7425/8c8ba656-2068-3208-b29a-d8e3f6e3bfbc.png[/img]
[/img]


6、通过上一篇对IOS应用程序沙盒的的介绍,本工程属性文件data.plist,保存的位置是:

/Users/duobianxing/Library/Application Support/iPhone Simulator/5.0/Applications/88528A36-00CB-4900-9762-1FD5D6AC8595/Documents

[img]
[img]http://dl.iteye.com/upload/attachment/0078/7427/05d588f8-af3d-36fa-a5ef-3eeeb2e540c8.png[/img]
[/img]


7、总结:

1)可以简单保存静态数据,但是无法将自定义对象序列化到属性列表中

2)该持久化方法与上一篇中介绍的NSUserDefaults实质上都是将数据写入到.plist属性文件中,区别是文件保存的位置不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值