【问题标题】:Objective-C: writing values to a specific index of an Array inside a .plistObjective-C:将值写入 .plist 中数组的特定索引
【发布时间】:2012-10-09 10:04:04
【问题描述】:

我知道我可以例如将值写入 .plist 文件

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"stored" ofType:@"plist"];
NSString *comment = @"this is a comment"; 
[comment writeToFile:filePath atomically:YES];

但是如果我说我的 .plist (gameArray) 中有一个数组,我想将 comment 变成我数组的特定索引,即 gameArray[4] ;我该怎么做?

请允许我澄清一下

  • 我有一个plist:stored.plist
  • 在我的 plist 中有一个数组 gameArray
  • 我想更新 plist 中 gameArray 的特定索引 这可能吗 ?

【问题讨论】:

    标签: objective-c xcode nsarray nsdictionary writetofile


    【解决方案1】:

    您不能在应用程序的主包中更新和保存数据,而是必须在文档目录或其他目录中执行以下操作:

     NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *plistFilePath = [documentsDirectory stringByAppendingPathComponent:@"stored.plist"];
    
    if([[NSFileManager defaultManager] fileExistsAtPAth:plistFilePath]) 
    {//already exits
    
       NSMutableArray *data = [NSMutableArray arrayWithContentsOfFile:plistFilePath];
       //update your array here
       NSString *comment = @"this is a comment";
       [data replaceObjectAtIndex:4 withObject:comment];
    
       //write file here
       [data writeToFile:plistFilePath atomically:YES];
    }
    else{ //firstly take content from plist and then write file document directory 
    
     NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"stored" ofType:@"plist"];
     NSMutableArray *data = [NSMutableArray arrayWithContentsOfFile:plistPath];
     //update your array here
       NSString *comment = @"this is a comment";
       [data replaceObjectAtIndex:4 withObject:comment];
    
       //write file here
       [data writeToFile:plistFilePath atomically:YES];
    }
    

    【讨论】:

    • 谢谢你,但你的代码正在做的是在我的 plist 中添加一个数组 data,我已经有一个数组,我正在尝试更新它。 . . .
    • 首先找到你的根结构,说它的数组或字典,如下所示: NSMutableArray *data = [NSMutableArray arrayWithContentsOfFile:plistPath];或 NSMutableDictionary *data = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
    • 你的结构将是带有数组的字典
    【解决方案2】:

    假设'stored.plist'的内容是一个数组,你需要从路径中实例化一个可变数组:

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"stored" ofType:@"plist"];
    NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:filePath];
    NSString *comment = @"this is a comment"; 
    
    // inserting a new object:
    [array insertObject:comment atIndex:4];
    
    // replacing an existing object:
    // classic obj-c syntax
    [array replaceObjectAtIndex:4 withObject:4];        
    // obj-c literal syntax:
    array[4] = comment;
    
    // Cannot save to plist inside your document bundle.
    // Save a copy inside ~/Library/Application Support
    
    NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask] objectAtIndex:0];
    NSURL *arrayURL = [documentsURL URLByAppendingPathComponent:[filePath lastPathComponent]];
    [array writeToURL:arrayURL atomically:NO];
    

    【讨论】:

    • 是的,但现在你只有一个数据类型 array 和索引 4 处的 comment,这不会写入 .plist,我想写入实际的 .plist
    • 添加了示例代码以保存到 ~/Library/Application Support/stored.plist
    • 是的,但是这会在 .plist 中添加一个数组,它不会更新现有的:是否也不能更新现有的?
    • 首次运行时,您可以将默认 plist 从文档包复制到 ~/Library/Application Support,然后更新并保存到该位置。
    猜你喜欢
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    • 2016-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-23
    相关资源
    最近更新 更多