2010-05-11 65 views
1

這裏是我能得到的值:有沒有簡單的方法來取代plist中的一個數據?

NSString *path = [[NSBundle mainBundle] pathForResource:@"myPlist" ofType:@"plist"]; 
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path]; 
NSArray *tempArray = [dict valueForKey:@"2"]; 
NSString *myTarget = [tempArray objectAtIndex:0]; 

的「myTarget」是我想更換,我怎麼能與@「newString」代替,並寫入的plist?你好。

回答

3

這樣做:

NSString *path = [[NSBundle mainBundle] pathForResource:@"myPlist" ofType:@"plist"]; 
NSData * data = [NSData dataWithContentsOfFile:path]; 
NSMutableDictionary * dict = (NSMutableDictionary *)[NSPropertyListSerialization 
            propertyListFromData:data 
            mutabilityOption:NSPropertyListMutableContainersAndLeaves 
            format:NULL 
            errorDescription:NULL]; 
NSMutableArray *tempArray = [dict valueForKey:@"2"]; 
NSString *myTarget = [tempArray objectAtIndex:0]; 
[tempArray replaceObjectAtIndex:0 withObject:newString]; 
[dict writeToFile:[path stringByExpandingTildeInPath] atomically:YES]; 
相關問題