iOS 数据存储
iOS 数据存储
iOS数据存储包括以下几种存储机制:
属性列表
对象归档
SQLite3
CoreData
AppSettings
普通文件存储
1、属性列表
- //
- // Persistence1ViewController.h
- // Persistence1
- //
- // Created by liu lavy on 11-10-3.
- // Copyright 2011 __MyCompanyName__. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- #define kFilename @"data.plist"
- @interface Persistence1ViewController : UIViewController {
- UITextField *filed1;
- UITextField *field2;
- UITextField *field3;
- UITextField *field4;
- }
- @property (nonatomic, retain) IBOutlet UITextField *field1;
- @property (nonatomic, retain) IBOutlet UITextField *field2;
- @property (nonatomic, retain) IBOutlet UITextField *field3;
- @property (nonatomic, retain) IBOutlet UITextField *field4;
- - (NSString *)dataFilePath;
- - (void)applicationWillResignActive:(NSNotification *)notification;
- @end
- //
- // Persistence1ViewController.m
- // Persistence1
- //
- // Created by liu lavy on 11-10-3.
- // Copyright 2011 __MyCompanyName__. All rights reserved.
- //
- #import "Persistence1ViewController.h"
- @implementation Persistence1ViewController
- @synthesize field1;
- @synthesize field2;
- @synthesize field3;
- @synthesize field4;
- //数据文件的完整路径
- - (NSString *)dataFilePath {
- //检索Documents目录路径。第二个参数表示将搜索限制在我们的应用程序沙盒中
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUSErDomainMask, YES);
- //每个应用程序只有一个Documents目录
- 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];
- [array release];
- }
- /*
- // The designated initializer. Override to perform setup that is required before the view is loaded.
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- */
- /*
- // Implement loadView to create a view hierarchy programmatically, without using a nib.
- - (void)loadView {
- }
- */
- // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- - (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];
- [array release];
- }
- UIApplication *app = [UIApplication sharedApplication];
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(applicationWillResignActive:)
- name:UIApplicationWillResignActiveNotification
- object:app];
- [super viewDidLoad];
- }
- /*
- // Override to allow orientations other than the default portrait orientation.
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
- // Return YES for supported orientations
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
- */
- - (void)didReceiveMemoryWarning {
- // Releases the view if it doesn't have a superview.
- [super didReceiveMemoryWarning];
- // Release any cached data, images, etc that aren't in use.
- }
- - (void)viewDidUnload {
- self.field1 = nil;
- self.field2 = nil;
- self.field3 = nil;
- self.field4 = nil;
- [super viewDidUnload];
- }
- - (void)dealloc {
- [field1 release];
- [field2 release];
- [field3 release];
- [field4 release];
- [super dealloc];
- }
- @end
|
评论暂时关闭