IOS Plist操作

代码:copy BUNDLE下的plist文件 到 library下面。

bundle下不支持些,library,doc路径支持读与写。

+ (void)copyUserpigListToLibrary {

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);

    NSString *libraryDirectory = [paths objectAtIndex:0];

    NSString *plistPath = [libraryDirectory stringByAppendingPathComponent:plistName];

    

    if (![fileManager fileExistsAtPath: plistPath]) {

        NSString *bundlePlistName =[[NSBundle mainBundle] pathForResource:@"userPig"ofType:@"plist"];

        NSData *mainBundleFile = [NSData dataWithContentsOfFile:bundlePlistName];

        [[NSFileManager defaultManager] createFileAtPath:plistPath contents:mainBundleFile attributes:nil];

    }

}

 

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:plistName];

    if ([fileManager fileExistsAtPath: plistPath]){

        NSMutableDictionary *  dict = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];

        NSString *pName = [dict objectForKey:userCityName];

        NSString *pId = [dict objectForKey:userCityID];

        if(![pName isEqualToString:city.cityName] || ![pId isEqualToString:city.cityID]) {

            [dict setValue:city.cityName forKey:userCityName];

            [dict setValue:city.cityID forKey:userCityID];

            [dict writeToFile:plistPath  atomically:YES];

        }

        [dict release];

    }

    else {

        NSString *resourceSampleImagesFolderPath =[[NSBundle mainBundle] pathForResource:@"userPig"ofType:@"plist"];

        NSData *mainBundleFile = [NSData dataWithContentsOfFile:resourceSampleImagesFolderPath];

        [[NSFileManager defaultManager] createFileAtPath:plistPath contents:mainBundleFile attributes:nil];

        NSMutableDictionary *  dict = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];

        NSString *pName = [dict objectForKey:userCityName];

        NSString *pID = [dict objectForKey:userCityID];

        if(![pName isEqualToString:city.cityName] || ![pID isEqualToString:city.cityID]) {

            [dict setValue:city.cityName forKey:userCityName];

            [dict setValue:city.cityID forKey:userCityID];

            [dict writeToFile:plistPath atomically:YES];

        }

        [dict release];

    }

 

你可能感兴趣的:(plist)