Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

iphone - modifying a plist is not working

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 updated with the new value ,but the problem is when i check the plist in bundle it is not being modified.any clue on why it is not updating the plist

  NSString* plistPath = nil;
        NSFileManager* manager = [NSFileManager defaultManager];
        if (plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"TopicsList.plist"]) 
        {
            if ([manager isWritableFileAtPath:plistPath]) 
            {
                NSMutableArray* dictarrays = [NSMutableArray arrayWithContentsOfFile:plistPath];
                NSMutableDictionary *dict=[dictarrays objectAtIndex:indexPath.row];
                NSLog(@"%@ ",dict );

                [dict setObject:[NSNumber numberWithBool:YES] forKey:@"isPaid"];

                NSLog(@"%@ ",dict );
                [dict writeToFile:plistPath atomically:NO];
                    NSLog(@"%@ ",dict );
                [manager setAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] ofItemAtPath:[[NSBundle mainBundle] bundlePath] error:&error];
            }
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...