- 浏览: 12933149 次
- 性别:
- 来自: 大连
文章分类
最新评论
-
sanrenxing_1:
GoEasy 实时推送支持IE6-IE11及大多数主流浏览器的 ...
WindowsPhone消息推送服务 -
张砚辉:
两侧照片绕Y轴旋转后有锯齿,请问锯齿解决方案,很长时间没解决
自定义带倒影和偏转的超炫Gallery -
knight_black_bob:
能不能把你自己的博客整理下分类下,写了这么多 ,都不知道怎么查 ...
Android_View,ViewGroup,Window之间的关系 -
jeasonyoung:
你这个代码实现在iOS8下应该是滑不动的
UISlider 滑块控件—IOS开发 -
wx_hello:
如果能写个可运行的java程序,不胜感激。。。
rs232串口通信原理
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
- //
- //Fourlines.m
- //Persistence2
- //
- //Createdbyliulavyon11-10-3.
- //Copyright2011__MyCompanyName__.Allrightsreserved.
- //
- #import"Fourlines.h"
- #definekField1Key@"Field1"
- #definekField2Key@"Field2"
- #definekField3Key@"Field3"
- #definekField4Key@"Field4"
- @implementationFourlines
- @synthesizefield1;
- @synthesizefield2;
- @synthesizefield3;
- @synthesizefield4;
- #pragmamarkNSCoding
- -(void)encodeWithCoder:(NSCoder*)aCoder{
- [aCoderencodeObject:field1forKey:kField1Key];
- [aCoderencodeObject:field2forKey:kField2Key];
- [aCoderencodeObject:field3forKey:kField3Key];
- [aCoderencodeObject:field4forKey:kField4Key];
- }
- -(id)initWithCoder:(NSCoder*)aDecoder{
- if(self=[superinit]){
- field1=[[aDecoderdecodeObjectForKey:kField1Key]retain];
- field2=[[aDecoderdecodeObjectForKey:kField2Key]retain];
- field3=[[aDecoderdecodeObjectForKey:kField3Key]retain];
- field4=[[aDecoderdecodeObjectForKey:kField4Key]retain];
- }
- returnself;
- }
- #pragmamark-
- #pragmamarkNSCopying
- -(id)copyWithZone:(NSZone*)zone{
- Fourlines*copy=[[[selfclass]allocWithZone:zone]init];
- copy.field1=[[self.field1copyWithZone:zone]autorelease];
- copy.field2=[[self.field2copyWithZone:zone]autorelease];
- copy.field3=[[self.field3copyWithZone:zone]autorelease];
- copy.field4=[[self.field4copyWithZone:zone]autorelease];
- returncopy;
- }
- @end
- //
- //Persistence2ViewController.h
- //Persistence2
- //
- //Createdbyliulavyon11-10-3.
- //Copyright2011__MyCompanyName__.Allrightsreserved.
- //
- #import<UIKit/UIKit.h>
- #definekFilename@"archive"
- #definekDataKey@"Data"
- @interfacePersistence2ViewController: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
- //
- //Persistence2ViewController.m
- //Persistence2
- //
- //Createdbyliulavyon11-10-3.
- //Copyright2011__MyCompanyName__.Allrightsreserved.
- //
- #import"Persistence2ViewController.h"
- #import"Fourlines.h"
- @implementationPersistence2ViewController
- @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{
- Fourlines*fourlines=[[Fourlinesalloc]init];
- fourlines.field1=field1.text;
- fourlines.field2=field2.text;
- fourlines.field3=field3.text;
- fourlines.field4=field4.text;
- NSMutableData*data=[[NSMutableDataalloc]init];//用于存储编码的数据
- NSKeyedArchiver*archiver=[[NSKeyedArchiveralloc]initForWritingWithMutableData:data];
- [archiverencodeObject:fourlinesforKey:kDataKey];
- [archiverfinishEncoding];
- [datawriteToFile:[selfdataFilePath]atomically:YES];
- [fourlinesrelease];
- [archiverrelease];
- [datarelease];
- }
- /*
- //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]){
- //从文件获取用于解码的数据
- NSData*data=[[NSMutableDataalloc]initWithContentsOfFile:[selfdataFilePath]];
- NSKeyedUnarchiver*unarchiver=[[NSKeyedUnarchiveralloc]initForReadingWithData:data];
- Fourlines*fourlines=[unarchiverdecodeObjectForKey:kDataKey];
- [unarchiverfinishDecoding];
- field1.text=fourlines.field1;
- field2.text=fourlines.field2;
- field3.text=fourlines.field3;
- field4.text=fourlines.field4;
- [unarchiverrelease];
- [datarelease];
- }
- 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
===================================================================================
===================================================================================
3、SQLite
- //
- //Persistence3ViewController.h
- //Persistence3
- //
- //Createdbyliulavyon11-10-3.
- //Copyright2011__MyCompanyName__.Allrightsreserved.
- //
- #import<UIKit/UIKit.h>
- #definekFilename@"data.sqlite3"
- @interfacePersistence3ViewController: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
- //
- //Persistence3ViewController.m
- //Persistence3
- //
- //Createdbyliulavyon11-10-3.
- //Copyright2011__MyCompanyName__.Allrightsreserved.
- //
- #import"Persistence3ViewController.h"
- #import<sqlite3.h>
- @implementationPersistence3ViewController
- @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{
- sqlite3*database;
- if(sqlite3_open([[selfdataFilePath]UTF8String],&database)!=SQLITE_OK){
- sqlite3_close(database);
- NSAssert(0,@"Failedtoopendatabase");
- }
- for(inti=1;i<=4;i++){
- NSString*fieldname=[[NSStringalloc]initWithFormat:@"field%d",i];
- UITextField*field=[selfvalueForKey:fieldname];
- [fieldnamerelease];
- char*update="INSERTORREPLACEINTOFIELDS(ROW,FIELD_DATA)VALUES(?,?);";
- sqlite3_stmt*stmt;
- //将SQL语句编译为sqlite内部一个结构体(sqlite3_stmt),类似javaJDBC的PreparedStatement预编译
- if(sqlite3_prepare_v2(database,update,-1,&stmt,nil)==SQLITE_OK){
- //在bind参数的时候,参数列表的index从1开始,而取出数据的时候,列的index是从0开始
- sqlite3_bind_int(stmt,1,i);
- sqlite3_bind_text(stmt,2,[field.textUTF8String],-1,NULL);
- }else{
- NSAssert(0,@"Error:Failedtopreparestatemen");
- }
- //执行SQL文,获取结果
- intresult=sqlite3_step(stmt);
- if(result!=SQLITE_DONE){
- NSAssert1(0,@"Errorupdatingtable:%d",result);
- }
- //释放stmt占用的内存(sqlite3_prepare_v2()分配的)
- sqlite3_finalize(stmt);
- }
- sqlite3_close(database);
- }
- /*
- //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]){
- //打开数据库
- sqlite3*database;
- if(sqlite3_open([filePathUTF8String],&database)!=SQLITE_OK){
- sqlite3_close(database);
- NSAssert(0,@"Failedtoopendatabase");
- }
- //创建表
- char*errorMsg;
- NSString*createSQL=
- @"CREATETABLEIFNOTEXISTSFIELDS(ROWINTEGERPRIMARYKEY,FIELD_DATATEXT);";
- if(sqlite3_exec(database,[createSQLUTF8String],NULL,NULL,&errorMsg)!=SQLITE_OK){
- sqlite3_close(database);
- NSAssert(0,@"Errorcreatingtable:%s",errorMsg);
- }
- //查询
- NSString*query=@"SELECTROW,FIELD_DATAFROMFIELDSORDERBYROW";
- sqlite3_stmt*statement;
- //设置nByte可以加速操作
- if(sqlite3_prepare_v2(database,[queryUTF8String],-1,&statement,nil)==SQLITE_OK){
- while(sqlite3_step(statement)==SQLITE_ROW){//返回每一行
- introw=sqlite3_column_int(statement,0);
- char*rowData=(char*)sqlite3_column_text(statement,1);
- NSString*fieldName=[[NSStringalloc]initWithFormat:@"field%d",row];
- NSString*fieldValue=[[NSStringalloc]initWithUTF8String:rowData];
- UITextField*field=[selfvalueForKey:fieldName];
- field.text=fieldValue;
- [fieldNamerelease];
- [fieldValuerelease];
- }
- //释放statement占用的内存(sqlite3_prepare()分配的)
- sqlite3_finalize(statement);
- }
- sqlite3_close(database);
- }
- 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
===================================================================================
===================================================================================
4、Core Data
- //
- //PersistenceViewController.h
- //Persistence4
- //
- //Createdbyliulavyon11-10-3.
- //Copyright2011__MyCompanyName__.Allrightsreserved.
- //
- #import<UIKit/UIKit.h>
- @interfacePersistenceViewController: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;
- @end
- //
- //PersistenceViewController.m
- //Persistence4
- //
- //Createdbyliulavyon11-10-3.
- //Copyright2011__MyCompanyName__.Allrightsreserved.
- //
- #import"PersistenceViewController.h"
- #import"Persistence4AppDelegate.h"
- @implementationPersistenceViewController
- @synthesizefield1;
- @synthesizefield2;
- @synthesizefield3;
- @synthesizefield4;
- -(void)applicationWillResignActive:(NSNotification*)notification{
- Persistence4AppDelegate*appDelegate=[[UIApplicationsharedApplication]delegate];
- NSManagedObjectContext*context=[appDelegatemanagedObjectContext];
- NSError*error;
- for(inti=1;i<=4;i++){
- NSString*fieldName=[NSStringstringWithFormat:@"field%d",i];
- UITextField*theField=[selfvalueForKey:fieldName];
- //创建提取请求
- NSFetchRequest*request=[[NSFetchRequestalloc]init];
- //创建实体描述并关联到请求
- NSEntityDescription*entityDescription=[NSEntityDescriptionentityForName:@"Line"
- inManagedObjectContext:context];
- [requestsetEntity:entityDescription];
- //设置检索数据的条件
- NSPredicate*pred=[NSPredicatepredicateWithFormat:@"(lineNum=%d)",i];
- [requestsetPredicate:pred];
- NSManagedObject*theLine=nil;
- ////检查是否返回了标准匹配的对象,如果有则加载它,如果没有则创建一个新的托管对象来保存此字段的文本
- NSArray*objects=[contextexecuteFetchRequest:requesterror:&error];
- if(!objects){
- NSLog(@"Therewasanerror");
- }
- //if(objects.count>0){
- //theLine=[objectsobjectAtIndex:0];
- //}else{
- //创建一个新的托管对象来保存此字段的文本
- theLine=[NSEntityDescriptioninsertNewObjectForEntityForName:@"Line"
- inManagedObjectContext:context];
- [theLinesetValue:[NSNumbernumberWithInt:i]forKey:@"lineNum"];
- [theLinesetValue:theField.textforKey:@"lineText"];
- //}
- [requestrelease];
- }
- //通知上下文保存更改
- [contextsave:&error];
- }
- //Thedesignatedinitializer.OverrideifyoucreatethecontrollerprogrammaticallyandwanttoperformcustomizationthatisnotappropriateforviewDidLoad.
- /*
- -(id)initWithNibName:(NSString*)nibNameOrNilbundle:(NSBundle*)nibBundleOrNil{
- self=[superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];
- if(self){
- //Custominitialization.
- }
- returnself;
- }
- */
- //ImplementviewDidLoadtodoadditionalsetupafterloadingtheview,typicallyfromanib.
- -(void)viewDidLoad{
- Persistence4AppDelegate*appDelegate=[[UIApplicationsharedApplication]delegate];
- NSManagedObjectContext*context=[appDelegatemanagedObjectContext];
- //创建一个实体描述
- NSEntityDescription*entityDescription=[NSEntityDescriptionentityForName:@"Line"inManagedObjectContext:context];
- //创建一个请求,用于提取对象
- NSFetchRequest*request=[[NSFetchRequestalloc]init];
- [requestsetEntity:entityDescription];
- //检索对象
- NSError*error;
- NSArray*objects=[contextexecuteFetchRequest:requesterror:&error];
- if(!objects){
- NSLog(@"Therewasanerror!");
- }
- for(NSManagedObject*objinobjects){
- NSNumber*lineNum=[objvalueForKey:@"lineNum"];
- NSString*lineText=[objvalueForKey:@"lineText"];
- NSString*fieldName=[NSStringstringWithFormat:@"field%d",[lineNumintegerValue]];
- UITextField*textField=[selfvalueForKey:fieldName];
- textField.text=lineText;
- }
- [requestrelease];
- 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,etc.thataren'tinuse.
- }
- -(void)viewDidUnload{
- self.field1=nil;
- self.field2=nil;
- self.field3=nil;
- self.field4=nil;
- [superviewDidUnload];
- }
- -(void)dealloc{
- [field1release];
- [field2release];
- [field3release];
- [field4release];
- [superdealloc];
- }
- @end
5、AppSettings
- NSUserDefaults*defaults=[NSUserDefaultsstandardUserDefaults];
===================================================================================
===================================================================================
6、普通文件存储
这种方式即自己将数据通过IO保存到文件,或从文件读取。
声明:此篇所有代码来自《Iphone4与IPad开发基础教程》
属性列表
对象归档
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
- //
- //Fourlines.m
- //Persistence2
- //
- //Createdbyliulavyon11-10-3.
- //Copyright2011__MyCompanyName__.Allrightsreserved.
- //
- #import"Fourlines.h"
- #definekField1Key@"Field1"
- #definekField2Key@"Field2"
- #definekField3Key@"Field3"
- #definekField4Key@"Field4"
- @implementationFourlines
- @synthesizefield1;
- @synthesizefield2;
- @synthesizefield3;
- @synthesizefield4;
- #pragmamarkNSCoding
- -(void)encodeWithCoder:(NSCoder*)aCoder{
- [aCoderencodeObject:field1forKey:kField1Key];
- [aCoderencodeObject:field2forKey:kField2Key];
- [aCoderencodeObject:field3forKey:kField3Key];
- [aCoderencodeObject:field4forKey:kField4Key];
- }
- -(id)initWithCoder:(NSCoder*)aDecoder{
- if(self=[superinit]){
- field1=[[aDecoderdecodeObjectForKey:kField1Key]retain];
- field2=[[aDecoderdecodeObjectForKey:kField2Key]retain];
- field3=[[aDecoderdecodeObjectForKey:kField3Key]retain];
- field4=[[aDecoderdecodeObjectForKey:kField4Key]retain];
- }
- returnself;
- }
- #pragmamark-
- #pragmamarkNSCopying
- -(id)copyWithZone:(NSZone*)zone{
- Fourlines*copy=[[[selfclass]allocWithZone:zone]init];
- copy.field1=[[self.field1copyWithZone:zone]autorelease];
- copy.field2=[[self.field2copyWithZone:zone]autorelease];
- copy.field3=[[self.field3copyWithZone:zone]autorelease];
- copy.field4=[[self.field4copyWithZone:zone]autorelease];
- returncopy;
- }
- @end
- //
- //Persistence2ViewController.h
- //Persistence2
- //
- //Createdbyliulavyon11-10-3.
- //Copyright2011__MyCompanyName__.Allrightsreserved.
- //
- #import<UIKit/UIKit.h>
- #definekFilename@"archive"
- #definekDataKey@"Data"
- @interfacePersistence2ViewController: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
- //
- //Persistence2ViewController.m
- //Persistence2
- //
- //Createdbyliulavyon11-10-3.
- //Copyright2011__MyCompanyName__.Allrightsreserved.
- //
- #import"Persistence2ViewController.h"
- #import"Fourlines.h"
- @implementationPersistence2ViewController
- @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{
- Fourlines*fourlines=[[Fourlinesalloc]init];
- fourlines.field1=field1.text;
- fourlines.field2=field2.text;
- fourlines.field3=field3.text;
- fourlines.field4=field4.text;
- NSMutableData*data=[[NSMutableDataalloc]init];//用于存储编码的数据
- NSKeyedArchiver*archiver=[[NSKeyedArchiveralloc]initForWritingWithMutableData:data];
- [archiverencodeObject:fourlinesforKey:kDataKey];
- [archiverfinishEncoding];
- [datawriteToFile:[selfdataFilePath]atomically:YES];
- [fourlinesrelease];
- [archiverrelease];
- [datarelease];
- }
- /*
- //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]){
- //从文件获取用于解码的数据
- NSData*data=[[NSMutableDataalloc]initWithContentsOfFile:[selfdataFilePath]];
- NSKeyedUnarchiver*unarchiver=[[NSKeyedUnarchiveralloc]initForReadingWithData:data];
- Fourlines*fourlines=[unarchiverdecodeObjectForKey:kDataKey];
- [unarchiverfinishDecoding];
- field1.text=fourlines.field1;
- field2.text=fourlines.field2;
- field3.text=fourlines.field3;
- field4.text=fourlines.field4;
- [unarchiverrelease];
- [datarelease];
- }
- 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
===================================================================================
===================================================================================
3、SQLite
- //
- //Persistence3ViewController.h
- //Persistence3
- //
- //Createdbyliulavyon11-10-3.
- //Copyright2011__MyCompanyName__.Allrightsreserved.
- //
- #import<UIKit/UIKit.h>
- #definekFilename@"data.sqlite3"
- @interfacePersistence3ViewController: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
- //
- //Persistence3ViewController.m
- //Persistence3
- //
- //Createdbyliulavyon11-10-3.
- //Copyright2011__MyCompanyName__.Allrightsreserved.
- //
- #import"Persistence3ViewController.h"
- #import<sqlite3.h>
- @implementationPersistence3ViewController
- @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{
- sqlite3*database;
- if(sqlite3_open([[selfdataFilePath]UTF8String],&database)!=SQLITE_OK){
- sqlite3_close(database);
- NSAssert(0,@"Failedtoopendatabase");
- }
- for(inti=1;i<=4;i++){
- NSString*fieldname=[[NSStringalloc]initWithFormat:@"field%d",i];
- UITextField*field=[selfvalueForKey:fieldname];
- [fieldnamerelease];
- char*update="INSERTORREPLACEINTOFIELDS(ROW,FIELD_DATA)VALUES(?,?);";
- sqlite3_stmt*stmt;
- //将SQL语句编译为sqlite内部一个结构体(sqlite3_stmt),类似javaJDBC的PreparedStatement预编译
- if(sqlite3_prepare_v2(database,update,-1,&stmt,nil)==SQLITE_OK){
- //在bind参数的时候,参数列表的index从1开始,而取出数据的时候,列的index是从0开始
- sqlite3_bind_int(stmt,1,i);
- sqlite3_bind_text(stmt,2,[field.textUTF8String],-1,NULL);
- }else{
- NSAssert(0,@"Error:Failedtopreparestatemen");
- }
- //执行SQL文,获取结果
- intresult=sqlite3_step(stmt);
- if(result!=SQLITE_DONE){
- NSAssert1(0,@"Errorupdatingtable:%d",result);
- }
- //释放stmt占用的内存(sqlite3_prepare_v2()分配的)
- sqlite3_finalize(stmt);
- }
- sqlite3_close(database);
- }
- /*
- //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]){
- //打开数据库
- sqlite3*database;
- if(sqlite3_open([filePathUTF8String],&database)!=SQLITE_OK){
- sqlite3_close(database);
- NSAssert(0,@"Failedtoopendatabase");
- }
- //创建表
- char*errorMsg;
- NSString*createSQL=
- @"CREATETABLEIFNOTEXISTSFIELDS(ROWINTEGERPRIMARYKEY,FIELD_DATATEXT);";
- if(sqlite3_exec(database,[createSQLUTF8String],NULL,NULL,&errorMsg)!=SQLITE_OK){
- sqlite3_close(database);
- NSAssert(0,@"Errorcreatingtable:%s",errorMsg);
- }
- //查询
- NSString*query=@"SELECTROW,FIELD_DATAFROMFIELDSORDERBYROW";
- sqlite3_stmt*statement;
- //设置nByte可以加速操作
- if(sqlite3_prepare_v2(database,[queryUTF8String],-1,&statement,nil)==SQLITE_OK){
- while(sqlite3_step(statement)==SQLITE_ROW){//返回每一行
- introw=sqlite3_column_int(statement,0);
- char*rowData=(char*)sqlite3_column_text(statement,1);
- NSString*fieldName=[[NSStringalloc]initWithFormat:@"field%d",row];
- NSString*fieldValue=[[NSStringalloc]initWithUTF8String:rowData];
- UITextField*field=[selfvalueForKey:fieldName];
- field.text=fieldValue;
- [fieldNamerelease];
- [fieldValuerelease];
- }
- //释放statement占用的内存(sqlite3_prepare()分配的)
- sqlite3_finalize(statement);
- }
- sqlite3_close(database);
- }
- 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
===================================================================================
===================================================================================
4、Core Data
- //
- //PersistenceViewController.h
- //Persistence4
- //
- //Createdbyliulavyon11-10-3.
- //Copyright2011__MyCompanyName__.Allrightsreserved.
- //
- #import<UIKit/UIKit.h>
- @interfacePersistenceViewController: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;
- @end
- //
- //PersistenceViewController.m
- //Persistence4
- //
- //Createdbyliulavyon11-10-3.
- //Copyright2011__MyCompanyName__.Allrightsreserved.
- //
- #import"PersistenceViewController.h"
- #import"Persistence4AppDelegate.h"
- @implementationPersistenceViewController
- @synthesizefield1;
- @synthesizefield2;
- @synthesizefield3;
- @synthesizefield4;
- -(void)applicationWillResignActive:(NSNotification*)notification{
- Persistence4AppDelegate*appDelegate=[[UIApplicationsharedApplication]delegate];
- NSManagedObjectContext*context=[appDelegatemanagedObjectContext];
- NSError*error;
- for(inti=1;i<=4;i++){
- NSString*fieldName=[NSStringstringWithFormat:@"field%d",i];
- UITextField*theField=[selfvalueForKey:fieldName];
- //创建提取请求
- NSFetchRequest*request=[[NSFetchRequestalloc]init];
- //创建实体描述并关联到请求
- NSEntityDescription*entityDescription=[NSEntityDescriptionentityForName:@"Line"
- inManagedObjectContext:context];
- [requestsetEntity:entityDescription];
- //设置检索数据的条件
- NSPredicate*pred=[NSPredicatepredicateWithFormat:@"(lineNum=%d)",i];
- [requestsetPredicate:pred];
- NSManagedObject*theLine=nil;
- ////检查是否返回了标准匹配的对象,如果有则加载它,如果没有则创建一个新的托管对象来保存此字段的文本
- NSArray*objects=[contextexecuteFetchRequest:requesterror:&error];
- if(!objects){
- NSLog(@"Therewasanerror");
- }
- //if(objects.count>0){
- //theLine=[objectsobjectAtIndex:0];
- //}else{
- //创建一个新的托管对象来保存此字段的文本
- theLine=[NSEntityDescriptioninsertNewObjectForEntityForName:@"Line"
- inManagedObjectContext:context];
- [theLinesetValue:[NSNumbernumberWithInt:i]forKey:@"lineNum"];
- [theLinesetValue:theField.textforKey:@"lineText"];
- //}
- [requestrelease];
- }
- //通知上下文保存更改
- [contextsave:&error];
- }
- //Thedesignatedinitializer.OverrideifyoucreatethecontrollerprogrammaticallyandwanttoperformcustomizationthatisnotappropriateforviewDidLoad.
- /*
- -(id)initWithNibName:(NSString*)nibNameOrNilbundle:(NSBundle*)nibBundleOrNil{
- self=[superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];
- if(self){
- //Custominitialization.
- }
- returnself;
- }
- */
- //ImplementviewDidLoadtodoadditionalsetupafterloadingtheview,typicallyfromanib.
- -(void)viewDidLoad{
- Persistence4AppDelegate*appDelegate=[[UIApplicationsharedApplication]delegate];
- NSManagedObjectContext*context=[appDelegatemanagedObjectContext];
- //创建一个实体描述
- NSEntityDescription*entityDescription=[NSEntityDescriptionentityForName:@"Line"inManagedObjectContext:context];
- //创建一个请求,用于提取对象
- NSFetchRequest*request=[[NSFetchRequestalloc]init];
- [requestsetEntity:entityDescription];
- //检索对象
- NSError*error;
- NSArray*objects=[contextexecuteFetchRequest:requesterror:&error];
- if(!objects){
- NSLog(@"Therewasanerror!");
- }
- for(NSManagedObject*objinobjects){
- NSNumber*lineNum=[objvalueForKey:@"lineNum"];
- NSString*lineText=[objvalueForKey:@"lineText"];
- NSString*fieldName=[NSStringstringWithFormat:@"field%d",[lineNumintegerValue]];
- UITextField*textField=[selfvalueForKey:fieldName];
- textField.text=lineText;
- }
- [requestrelease];
- 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,etc.thataren'tinuse.
- }
- -(void)viewDidUnload{
- self.field1=nil;
- self.field2=nil;
- self.field3=nil;
- self.field4=nil;
- [superviewDidUnload];
- }
- -(void)dealloc{
- [field1release];
- [field2release];
- [field3release];
- [field4release];
- [superdealloc];
- }
- @end
5、AppSettings
- NSUserDefaults*defaults=[NSUserDefaultsstandardUserDefaults];
===================================================================================
===================================================================================
6、普通文件存储
这种方式即自己将数据通过IO保存到文件,或从文件读取。
声明:此篇所有代码来自《Iphone4与IPad开发基础教程》
相关推荐
所谓的持久化,就是将数据保存到硬盘中,使得在应用程序或机器重启后可以继续访问之前保存的数据。在iOS开发中,有很多数据持久化的方案(plist、preference(userdefault)、NScoder(归档))
ios 数据存储,数据持久化,包含Preference 、文件存储、归档、数据库 、 CoreData的使用
iOS 的数据存储方式的一些相关代码, 从网上下载FMDB的原码,将其拖入到项目中,然后在Link Binary With Libraries中添加libsqlite3.dylib,就可以用了
几种常见的数据存储方式,比如CoreData,SQLite,归档,NSUserdefaults。 都是很基本的用法,^_^ 个人博客: http://www.lrdup.net github: https://github.com/SmallMonster77
(FMDB + runtime)数据库操作框架 YUDBFramework
主要介绍了IOS 数据存储详解及实例代码的相关资料,需要的朋友可以参考下
iOS中数据存储方式详解
介绍了NSManagerFile的几种常用的方法,简单易用,欢迎下载
(FMDB + runtime)数据库操作框架 YUDBFramework 更多相关资料:http://my.oschina.net/u/868062/blog/495648
iOS中有五种持久化数据的方式:属性列表、对象归档、NSUserDefault、SQLite3和Core Data 本文章讲述通过属性列表的方式持久化数据,这个方法也是我们平时最经常用到的方式。比如应用程序的配置和个性化的设置,一般...
这是关于iOS数据存储,FMDB封装,SQL使用等,有需要的小伙伴可以看看!
iOS开发中数据的存储,代码不够完整,后续会补充
详细介绍:github.com/pro648/tips/wiki/数据存储之归档解档-NSKeyedArchiver-NSKeyedUnarchiver - archiveRootObject: toFile:不能决定如何处理归档的数据,直接被写入了文件。 - ...
http://blog.csdn.net/duxinfeng2010/article/category/1155790 有相关解释
CDT数据存储 应用程序使用Cloudant Sync在设备上存储,索引和查询本地JSON数据,并在许多设备之间同步数据。 同步是在应用程序的控制之下,而不是由基础系统控制。 在本地设备或远程数据库中,冲突也易于管理和解决...
ios数据存储之---SQLite3,打开或创建一个数据库,创建一张数据表,以及常用的增删改查。
IOS中数据的持久化保存这块内容,类似于Android中文件的几种常见的存储方式。...也就是说:IOS上数据存储,我们要了解的两点,数据存储格式(也就是存储机制),数据存储位置。1》文件如何存储(如上面4点)2》文件存储
使用sqlite3的简单封装,使用方法简易
简单的归档使用