iphone - writeToFile not saving new entry into plist

后端 未结 2 1747
梦谈多话
梦谈多话 2020-12-12 02:18

My writeToFile is not saving my data to my .plist.

- (IBAction)clickBtnDone:(id) sender {
 NSLog(@\"Done\");
 if ([txtGroupName.text length] > 0) {  
  [s         


        
相关标签:
2条回答
  • 2020-12-12 02:33
    NSString* plistPath = nil;
    NSFileManager* manager = [NSFileManager defaultManager];
     if ((plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"PathTo.plist"])) 
       {
        if ([manager isWritableFileAtPath:plistPath]) 
         {
           NSMutableDictionary* infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
          [infoDict setObject:@"foo object" forKey:@"fookey"];
          [infoDict writeToFile:plistPath atomically:NO];
          [manager setAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] ofItemAtPath:[[NSBundle mainBundle] bundlePath] error:nil];
         }
       }
    
    0 讨论(0)
  • 2020-12-12 02:58

    How you have initialized groupPath? It should be the path of document directory, not the path of resource directory.

    You should do something similar :

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:FILE_NAME];
    

    You can not edit the file that is present in the workspace.

    0 讨论(0)
提交回复
热议问题