[转]保存数据的一系列方法

本文介绍了在iOS应用开发中常见的几种数据存储方式,包括NSKeyedArchiver归档、NSUserDefaults及写入文件的方法,并提供了具体的实现代码示例。

 

保存数据的一系列方法

 http://cocoachina.com/bbs/read.php?tid-9414-fpage-0-toread--page-1.html
/*=======================================================
NSKeyedArchiver
========================================================*/ 
NSString * str = @"abc" ;
NSString * astr = @"efg" ;
NSArray * Array = [ NSArray arrayWithObjects : str, astr, nil ] ;
 
//Save 
NSString * Path = [ NSSearchPathForDirectoriesInDomains ( NSDocumentDirectory, NSUserDomainMask, YES ) objectAtIndex : 0 ] ; NSString * filename = [ PathstringByAppendingPathComponent : @"test" ] ;
[ NSKeyedArchiver archiveRootObject : Array toFile : filename ] ;
 
str = @"a" ;
astr = @"" ;
 
//load 
NSArray * arr = [ NSKeyedUnarchiver unarchiveObjectWithFile : filename ] ;
str = [ arr objectAtIndex : 0 ] ;
astr = [ arr objectAtIndex : 1 ] ;
 
NSLog ( @"str:%@" ,str) ;
NSLog ( @"astr:%@" ,astr) ;
 
 
 
/*=======================================================
NSUserDefaults
========================================================*/ 
NSString * str = @"abc" ;
NSString * astr = @"efg" ;
NSArray * Array = [ NSArray arrayWithObjects : str, astr, nil ] ;
 
//Save 
NSUserDefaults * SaveDefaults = [ NSUserDefaults standardUserDefaults ] ;
[ SaveDefaults setObject : Array forKey : @"SaveKey" ] ;
 
str = @"a" ;
astr = @"" ;
 
//load 
Array = [ SaveDefaults objectForKey : @"SaveKey" ] ;
str = [ Array objectAtIndex : 0 ] ;
astr = [ Array objectAtIndex : 1 ] ;
NSLog ( @"str:%@" ,str) ;
NSLog ( @"astr:%@" ,astr) ;
 
 
 
 
/*=======================================================
writeToFile:
========================================================*/ 
NSString * str = @"abc" ;
NSString * astr = @"efg" ;
NSArray * Array = [ NSArray arrayWithObjects : str, astr, nil ] ;
 
//Save 
NSArray * paths = NSSearchPathForDirectoriesInDomains ( NSDocumentDirectory, NSUserDomainMask, YES ) ;
NSString * documentsDirectory = [ paths objectAtIndex : 0 ] ;
if ( ! documentsDirectory) { 
NSLog ( @"Documents directory not found!" ) ;
} 
NSString * appFile = [ documentsDirectory stringByAppendingPathComponent :@"Savedatas.plist" ] ;
[ [ NSArray arrayWithObjects : Array ,nil ] writeToFile : appFile atomically : NO ] ; 
 
 
//load 
if ( [ [ NSFileManager defaultManager ] fileExistsAtPath : appFile] ) 
self .SaveDataArray = [ NSMutableArray arrayWithContentsOfFile : appFile] ; 
else 
self .SaveDataArray = [ NSMutableArray arrayWithContentsOfFile : [ [ NSBundlemainBundle ] pathForResource : @"Savedatas" ofType : @"plist" ] ] ;
NSArray * strArray = [ self .SaveDataArray objectAtIndex : 0 ] ;
 
str = [ strArray objectAtIndex : 0 ] ;
astr = [ strArray objectAtIndex : 1 ] ;
 
 
 
 
 
//坛子里的,搬过来。。。。。 
- ( BOOL ) writeApplicationData: ( NSDictionary * ) data writeFileName: ( NSString * )fileName 
{ 
NSArray * paths = NSSearchPathForDirectoriesInDomains ( NSDocumentDirectory, NSUserDomainMask, YES ) ;
NSString * documentsDirectory = [ paths objectAtIndex : 0 ] ;
if ( ! documentsDirectory) { 
NSLog ( @"Documents directory not found!" ) ;
return NO ;
} 
NSString * appFile = [ documentsDirectory stringByAppendingPathComponent : fileName ];
return ( [ data writeToFile : appFile atomically : YES ] ) ;
} 
 
- ( id ) readApplicationData: ( NSString * ) fileName 
{ 
NSArray * paths = NSSearchPathForDirectoriesInDomains ( NSDocumentDirectory, NSUserDomainMask, YES ) ;
NSString * documentsDirectory = [ paths objectAtIndex : 0 ] ;
NSString * appFile = [ documentsDirectory stringByAppendingPathComponent : fileName ];
NSDictionary * myData = [ [ [ NSDictionary alloc ] initWithContentsOfFile : appFile]autorelease ] ;
return myData;
} 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值