您的位置:首页 > 其它

iphone开发学习笔记-- 应用程序的沙盒(11)

2011-04-28 13:37 926 查看
3种持久保存数据的方法:属性列表,对象归档,SQLite3

(搞这么复杂的名字!!!不就是弄个ini或者conf配置文件就ok了!!!)

检索文档目录(这里是Document目录)路径的代码:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,

NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0);

在目录下建立一个文件:

NSString *filename = [documentsDirectory stringByAppending PathComponent:@"theFile.txt"];

获取tmp目录,并建立文件

NSString *tempPath = NSTemporaryDirectory();

NSString *tempFile = [tempPath stringByAppendingPathComponent:@"tempFile.txt"];

方法一:序列化

支持序列化的: NSArray, NSMutableArray, NSDictionary, NSMutableDictionary,NSData,NSMutableData,NSString,NSMutableString,NSNumber,NSDate

[myArray writeToFile:@"/some/output.plist" atomically:YES];

NSNotification 通知:是一种对象可以用于彼此通讯的轻量级机制。(起这么多别名,干嘛!!!不就是个信号吗!!!)

NSMutableArray 可变数组:

NSMutableArray *array = [[NSMutableArray alloc] init];

[array addObject:field1.text];//对应书中,前面定义过的field1

……

[array writeToFile:[self dataFilePath] atomically:YES];

[array release];

viewDidLoad函数:(就是个init函数!!!)

在该函数中,先判断是否有配置文件

if([[NSFileManager defaultManager] fileExistsAtPath:filePath]){

NSArray *array = [[NSArray alloc] initWithContentsOfFile:fielPath];

field1.text = [array objectAtIndex:0];

……

[array release];

}

UIApplication *app = [UIApplication shareApplication];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app];

好复杂的函数调用!!!有木有!!!变态!!!这个函数真垃圾!!!

不就相当于一个信号槽机制吗!!!

二:归档

归档就是更牛逼的序列化!!!他可以支持任何对象!!!(不就是个容器吗,不就是个模板吗!!!有必要这样叫吗,好像是你发明的多牛逼的东东!!!)

<NSCoding, NSCopying>需要这两个协议

貌似也挺麻烦的!!!还要自己先写一个类。

三。sqlite

sqlite3 *database;

int result = sqlite3_open("/path/to/database/file",&database);

char *cStringPath = [pathString UTF8String];//NSString 2 char*

sqlite3_close(database);

NSAssert(0,@"Failed to open database"); //断言。。。

看起来写个简单的程序,用个序列化就ok了。。保存点配置文件。
本文出自 “人生得意须尽欢” 博客,请务必保留此出处http://no001.blog.51cto.com/1142339/555486
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: