iPhone Dev - Edit objects in Array from Plist

前端 未结 1 624
-上瘾入骨i
-上瘾入骨i 2020-12-22 05:32

I have a Plist in which I have a number of Array\'s. Within these arrays are different objects that I\'m reading, I\'ve become stuck however on editing these objects in the

1条回答
  •  隐瞒了意图╮
    2020-12-22 06:16

    You cannot save data in application's main bundle instead you have to do in document directory like this:

     NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *plistFilePath = [documentsDirectory stringByAppendingPathComponent:@"Data.plist"];
    
    if([[NSFileManager defaultManager] fileExistsAtPAth:plistFilePath]) 
    {//plist already exits in doc dir so save content in it
    
     NSMutableArray *data = [NSMutableArray arrayWithContentsOfFile:plistFilePath];
     NSArray *arrValue = [data objectAtIndex:arrayCounter];
    
     NSString *strValue = [arrValue objectAtIndex:0];
     strValue = [strValue stringByAppending:@"hello"]; //change your value here
    
     [[data objectAtIndex:arrayCounter] replaceObjectAtIndex:0 withObject:strValue]; //value replaced
     [data writeToFile:plistFilePath atomically:YES];
    }
    else{ //firstly take content from plist and then write file document directory. it will be executed only once
    
     NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
     NSMutableArray *data = [NSMutableArray arrayWithContentsOfFile:plistPath];
     NSArray *arrValue = [data objectAtIndex:arrayCounter];
    
     NSString *strValue = [arrValue objectAtIndex:0];
     strValue = [strValue stringByAppending:@"hello"]; //change your value here
    
     [[data objectAtIndex:arrayCounter] replaceObjectAtIndex:0 withObject:strValue]; //value replaced
     [data writeToFile:plistFilePath atomically:YES];
    
    }
    

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