以代码实例总结iOS应用开发中数据的存储方式
ios数据存储包括以下几种存储机制:
- 属性列表
- 对象归档
- SQLite3
- CoreData
- AppSettings
- 普通文件存储
1、属性列表
//
// Persistence1ViewController.h
// Persistence1
//
// Createdbyliulavyon11-10-3.
// Copyright2011__MyCompanyName__.Allrightsreserved.
//
#import<UIKit/UIKit.h>
#definekFilename@"data.plist"
@interfacePersistence1ViewController:UIViewController{
UITextField*filed1;
UITextField*field2;
UITextField*field3;
UITextField*field4;
}
@property(nonatomic,retain)IBOutletUITextField*field1;
@property(nonatomic,retain)IBOutletUITextField*field2;
@property(nonatomic,retain)IBOutletUITextField*field3;
@property(nonatomic,retain)IBOutletUITextField*field4;
-(NSString*)dataFilePath;
-(void)applicationWillResignActive:(NSNotification*)notification;
@end
//
// Persistence1ViewController.m
// Persistence1
//
// Createdbyliulavyon11-10-3.
// Copyright2011__MyCompanyName__.Allrightsreserved.
//
#import"Persistence1ViewController.h"
@implementationPersistence1ViewController
@synthesizefield1;
@synthesizefield2;
@synthesizefield3;
@synthesizefield4;
//数据文件的完整路径
-(NSString*)dataFilePath{
//检索Documents目录路径。第二个参数表示将搜索限制在我们的应用程序沙盒中
NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
//每个应用程序只有一个Documents目录
NSString*documentsDirectory=[pathsobjectAtIndex:0];
//创建文件名
return[documentsDirectorystringByAppendingPathComponent:kFilename];
}
//应用程序退出时,将数据保存到属性列表文件
-(void)applicationWillResignActive:(NSNotification*)notification{
NSMutableArray*array=[[NSMutableArrayalloc]init];
[arrayaddObject:field1.text];
[arrayaddObject:field2.text];
[arrayaddObject:field3.text];
[arrayaddObject:field4.text];
[arraywriteToFile:[selfdataFilePath]atomically:YES];
[arrayrelease];
}
/*
//Thedesignatedinitializer.Overridetoperformsetupthatisrequiredbeforetheviewisloaded.
-(id)initWithNibName:(NSString*)nibNameOrNilbundle:(NSBundle*)nibBundleOrNil{
self=[superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];
if(self){
//Custominitialization
}
returnself;
}
*/
/*
//ImplementloadViewtocreateaviewhierarchyprogrammatically,withoutusinganib.
-(void)loadView{
}
*/
//ImplementviewDidLoadtodoadditionalsetupafterloadingtheview,typicallyfromanib.
-(void)viewDidLoad{
[superviewDidLoad];
NSString*filePath=[selfdataFilePath];
//检查数据文件是否存在
if([[NSFileManagerdefaultManager]fileExistsAtPath:filePath]){
NSArray*array=[[NSArrayalloc]initWithContentsOfFile:filePath];
field1.text=[arrayobjectAtIndex:0];
field2.text=[arrayobjectAtIndex:1];
field3.text=[arrayobjectAtIndex:2];
field4.text=[arrayobjectAtIndex:3];
[arrayrelease];
}
UIApplication*app=[UIApplicationsharedApplication];
[[NSNotificationCenterdefaultCenter]addObserver:self
selector:@selector(applicationWillResignActive:)
name:UIApplicationWillResignActiveNotification
object:app];
[superviewDidLoad];
}
/*
//Overridetoalloworientationsotherthanthedefaultportraitorientation.
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
//ReturnYESforsupportedorientations
return(interfaceOrientation==UIInterfaceOrientationPortrait);
}
*/
-(void)didReceiveMemoryWarning{
//Releasestheviewifitdoesn'thaveasuperview.
[superdidReceiveMemoryWarning];
//Releaseanycacheddata,images,etcthataren'tinuse.
}
-(void)viewDidUnload{
self.field1=nil;
self.field2=nil;
self.field3=nil;
self.field4=nil;
[superviewDidUnload];
}
-(void)dealloc{
[field1release];
[field2release];
[field3release];
[field4release];
[superdealloc];
}
@end
2、对象归档
//
// Fourlines.h
// Persistence2
//
// Createdbyliulavyon11-10-3.
// Copyright2011__MyCompanyName__.Allrightsreserved.
//
#import<Foundation/Foundation.h>
@interfaceFourlines:NSObject<NSCoding,NSCopying>{
NSString*field1;
NSString*field2;
NSString*field3;
NSString*field4;
}
@property(nonatomic,retain)NSString*field1;
@property(nonatomic,retain)NSString*field2;
@property(nonatomic,retain)NSString*field3;
@property(nonatomic,retain)NSString*field4;
@end