modifying a plist is not working

后端 未结 3 1343
陌清茗
陌清茗 2020-12-07 04:47

i have to modify a BOOL value in my plist file stored with the bundle.i am able to access the dictionary which i have to modify .from nslogging i can see that dict is update

相关标签:
3条回答
  • 2020-12-07 05:21

    Is the plist a part of your resources? Not sure if we can edit a plist there. Copy the plist to your app's Documents folder and update it there.

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *plistPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"TopicsList.plist"];
    
    success = [fileManager fileExistsAtPath:plistPath];
    if(!success){
        //file does not exist. So look into mainBundle
        NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"TopicsList.plist"];
        success = [fileManager copyItemAtPath:defaultPath toPath:plistPath error:&error];
    }
    

    Now whatever changes you need to make to the plist or read data from the plist, read it from the copy in Documents folder.

    0 讨论(0)
  • 2020-12-07 05:39

    You must store your plist in the documents directory. After that, you must also save the plist upon leaving the app. Otherwise, the plist is in the main bundle and cannot be modified.

    0 讨论(0)
  • 2020-12-07 05:43

    Is there any error

    Check with

    NSError *error = nil
    [dict writeToFile:plistPath atomically:YES encoding:NSASCIIStringEncoding error:&error];
    
    if (error) {
      NSLog(@"Error: %@", [error description]);
    } else {
    NSLog(@"Success");
    }
    
    0 讨论(0)
提交回复
热议问题