博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

在iPone环境下要保存数据

Posted on 2010-03-09 21:38  BradyChen  阅读(388)  评论(0编辑  收藏  举报
bool writeApplicationData(NSData *data, 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]);
    }
 
NSData *applicationDataFromFile(NSString *fileName)
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
        NSData *myData = [[[NSData alloc] initWithContentsOfFile:appFile] autorelease];
        return myData;
    }





2个方法都是存储在/Documents/里面