2011-06-16 71 views
1

我必须在我的plist文件中修改存储在bundle中的BOOL值。我能够访问我必须修改的字典。从nslogging中我可以看到该字典用新的值,但问题是,当我检查的plist捆绑是不是被modified.any线索,它为什么不更新的plist修改一个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]; 
      } 
     } 

回答

5

是plist中的资源的一部分吗?不知道我们是否可以在那里编辑plist。将plist复制到应用程序的Documents文件夹并在那里更新。

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]; 
} 

现在无论您需要对plist进行哪些更改或从plist读取数据,请从Documents文件夹中的副本中读取它。

+0

和@Nayefc这样就意味着我应该在Xcode中删除我的plist的参考和保存的plist在我的项目文件夹? – sujith1406 2011-06-16 08:32:24

+1

保留在那里。在应用程序启动时,将plist从您的包中复制到应用程序的Document文件夹(如果它尚不存在)。用相关的代码更新我的答案(应该在应用程序中去:didFinishLaunchingWithOptions: – lostInTransit 2011-06-16 10:09:56

0

您必须将plist存储在文档目录中。之后,您还必须在离开应用程序后保存plist。否则,plist在主包中,不能修改。

0

是否有任何错误

请与

NSError *error = nil 
[dict writeToFile:plistPath atomically:YES encoding:NSASCIIStringEncoding error:&error]; 

if (error) { 
    NSLog(@"Error: %@", [error description]); 
} else { 
NSLog(@"Success"); 
} 
相关问题