保存iPhone数据的代码
时间: 2010-01-27 11:27 点击: 次
作者 angellixf 原帖地址 http://www.cocoachina.com/bbs/read.php?tid-9414.html /*======================================================= NSKeyedArchiver ========================================================*/ NSString * str = @ abc ;
作者 angellixf
原帖地址 http://www.cocoachina.com/bbs/read.php?tid-9414.html
原帖地址 http://www.cocoachina.com/bbs/read.php?tid-9414.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 = [Path stringByAppendingPathComponent:@"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:[[NSBundle mainBundle] 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;
}
顶一下(2)
66.7%
踩一下(1)
33.3%
发表评论
本类文章点击排行榜
- [06-11] Objective-C语法快速参考
- [01-20] iPhone App开发官方视频教程
- [03-12] 中文版iTunes Connect开发者指南
- [06-11] Cocoa系列教学一:使用代码创
- [06-11] XCode 3.0及Cocoa初级教程
- [06-11] Cocoa系列教学二:Cocoa处理消息
- [04-02] 将自己开发的app打包成ipa装入
- [01-20] App推广码(promotion code)全面总
- [06-11] 初学者:介绍NSLog的使用
推荐内容
最近更新
- [07-13] 生成随机guid串的代码
- [07-12] UIScrollView里判断动画结束的方法
- [07-12] iPhone 开发中定义全局的结构指针
- [07-08] 一行代码判断运行应用的机器是IOS4还是IOS3
- [07-06] 动画循环播放并记录播放次数的代码
- [07-02] 使用 Xcode Debug 时查看全局变量的方法
- [06-29] 去掉App自动添加的图标半透明效果的方法
- [06-24] 苹果开发入门介绍:UINavigationController
- [06-23] Mac程序开发基础:得到一个目录内的内容


App推广码(promotion code)全面总结
iPhone App开发官方视频教程
Cocoa系列教学一:使用代码创建窗口
Objective-C语法快速参考
教学:如何实现拖拽文件到NSTableView中