iOS中可以有四种持久化数据的方式:属性列表、对象归档、SQLite3和Core Data
下面学习第一种:属性列表
ios应用程序沙盒(目录结构)
/Users/UserName/Library/Application Support/iPhone
Simulator/3.1.3/Applications
iOS应用程序采用沙盒原理设计,iOS每个应用程
1.序都有自己的3个目录(Documents,Library,tmp),互相之间不能访问。
2.Documents存放应用程序的数据。
3.Library目录下面还有Preferences和Caches目录,Preferences目录存放应用程序的使用偏好,Caches目录与Documents很相似可以存放应用程序的数据。
4.tmp目录供应用程序存储临时文件。
主要方法:获得应用程序的Documents文件夹
NSArray * myPaths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * myDocPath = [myPaths objectAtIndex:0];
获取文件的完整路径
NSString *filename = [myDocPath
stringByAppendingPathComponent:@"properties.plist"];
获取tmp目录
NSString *tempPath = NSTemporaryDirectory ();
//在该路径的结尾附加一个文件名,在该目录创建一个该文件的路径
NSString *tempFile = [tempPath stringByAppendingPathComponent]:@"tempFile.txt"
实例演示:
新建项目Persistence
BIDViewController.h文件
#import <UIKit/UIKit.h>
@interface BIDViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *field1;
@property (weak, nonatomic) IBOutlet UITextField *field2;
@property (weak, nonatomic) IBOutlet UITextField *field3;
@property (weak, nonatomic) IBOutlet UITextField *field4;
//该方法将return the full pathname to our data file by concatenating a file name onto the path for the Documents directory
- (NSString *)dataFilePath;
//应用程序在退出是调用该方法,将数据保存到属性列表
- (void)applicationWillResignActive:(NSNotification *)notification;
@end
BIDViewController.m文件
#import "BIDViewController.h"
#define kFilename @"data.plist"
@interface BIDViewController ()
@end
@implementation BIDViewController
@synthesize field1;
@synthesize field2;
@synthesize field3;
@synthesize field4;
- (NSString *)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFilename];
}
- (void)applicationWillResignActive:(NSNotification *)notification
{
}
#pragma mark -- lifecycle
- (void)viewDidLoad
{
// Do any additional setup after loading the view, typically from a nib.
//在viewDidload方法种,我们做几件事。第一是检查数据文件是否存在,如果不存在则不加载。若存在,就用该文件的内容实例化数组,然后将对象复制到4个文本字段。
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];
}
//加载数据后,获得程序实例的引用,并使该引用订阅UIApplicationWillResignActiveNotification,使用默认的NSNotificationCenter实例,及一个名为addObserver的方法。传递一个observer,即self UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillResignActive:)
name:UIApplicationWillResignActiveNotification
object:app];
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.field1 = nil;
self.field2 = nil;
self.field3 = nil;
self.field4 = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end