一.データ持続化app砂箱プロパティファイル保存

1610 ワード

データ持続化app砂箱プロパティファイル
self.plistFilePath =  [self applicationDocumentsDirectoryFile];

//         
- (void)createPropertyListFileIfNeeded {

    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL dbexits = [fileManager fileExistsAtPath:self.plistFilePath];
    if (!dbexits) {
        int method = 2;
        // 1  app         
        if(method == 1){
            //1.        NSBundle  
            NSBundle *frameworkBundle = [NSBundle bundleForClass:[ self class]];
            NSString *frameworkBundlePath = [frameworkBundle resourcePath];
            NSString *defaultDBPath = [frameworkBundlePath stringByAppendingPathComponent:@“dataList.plist"];
            NSError *error;
            //1.1           
            BOOL success = [fileManager copyItemAtPath:defaultDBPath toPath:self.plistFilePath error:&error];

            if (error) {
                NSAssert(success, @"      ");
            }
        }
        // 2        
        else if(method == 2) {
            NSMutableArray *array =  [[NSMutableArray alloc] initWithCapacity:0];
            //2.          (      ,  ,           )
            [array writeToFile:self.plistFilePath atomically:TRUE];
        }
    }
}

- (NSString *)applicationDocumentsDirectoryFile {
    NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, TRUE) lastObject];
    NSString *path = [documentDirectory stringByAppendingPathComponent:@"dataList.plist"];
    return path;
}

もしあなたが本文があなたに役立つことを発見したら、他の人も利益を得ることができると思ったら、それを分かち合ってください.