帮助字典和plist

时间:2011-07-19 17:34:49

标签: objective-c plist nsdictionary

我有一个包含字典项的.plist文件,如下所示:

<dict>
          <dict>
                     <key>Key one</key>
                     <string>string</string>
                     ...
          </dict>
          ...
</dict>

如何编辑Key one的字符串? 我被困在这里:

NSString *pathVibrate = [NSString stringWithString:@"plist path"
NSMutableDictionary *plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:pathVibrate];

EDIT。 plist是这样的:S

<dict>
          <key>Key One</key>
          <dict>
                     <key>Key one</key>
                     <string>string</string>
                     ...
          </dict>
          <key>Key Two</key>
          <dict>
                     <key>Key one</key>
                     <string>string</string>
                     ...
          </dict>
</dict>

2 个答案:

答案 0 :(得分:2)

不是100%肯定我知道你在追求什么,你的意思是你想要编辑关键字符串,或者存储在那个字符串中的字符串,我怀疑后者......

NSString *origString = [plistDict objectForKey:@"Key one"];
NSString *newString = [NSString stringWithFormat:@"Some change here to string =     %@",origString];
[plistDict setObject:newString forKey:@"Key one"]; // Will overwrite the original dictionary entry with the new string

好的,基于额外的信息,我认为你想要更改字典中的字典,在这种情况下,上面的代码需要一个小的改变......

NSMutableDictionary *subDictionary = [plistDict objectForKey:@"Key One"];
NSString *origString = [subDictionary objectForKey:@"Key one"];
NSString *newString = [NSString stringWithFormat:@"Some change here to string =     %@",origString];
[subDictionary setObject:newString forKey:@"Key one"]; // Will overwrite the original dictionary entry with the new string


subDictionary = [plistDict objectForKey:@"Key Two"];
NSString *origString = [subDictionary objectForKey:@"Key one"];
NSString *newString = [NSString stringWithFormat:@"Some other change here to string =     %@",origString];
[subDictionary setObject:newString forKey:@"Key one"]; // Will overwrite the original dictionary entry with the new string

这就是你追求的吗?在这种情况下,我不是100%肯定,但我不认为plistDict需要声明为Mutable,因为根字典本身不会改变,只有子字典。

你当然应该验证从[plistDict objectForKey:xxx]返回的指针不是nil,以确保密钥实际存在,然后再继续修改结果......

NSMutableDictionary *subDictionary = [plistDict objectForKey:@"Key One"];
if(subDictionary!=nil)
{
    NSString *origString = [subDictionary objectForKey:@"Key one"];
    if(origString!=nil)
    {
        NSString *newString = [NSString stringWithFormat:@"Some other change here to string =     %@",origString];
        [subDictionary setObject:newString forKey:@"Key one"]; // Will overwrite the original dictionary entry with the new string
    }
}

注意我曾经在Objective C书中读到,在目标C中,仅仅测试一个指针是不够的:

如果(子词典) { }

在布尔测试目标C中仅测试底部8位为非零值并忽略指针中的其余位,因此如果指针位于256字节边界上,则if将返回false事实上是真的。这可能已在Objective-C 2.0中得到解决,但我总是为了安全而使用长手版本。

答案 1 :(得分:1)

保存plist很简单,只需使用

即可
// for an array
BOOL result = [myArray writeToFile:filename atomically:YES];

// for a dictionary
BOOL result = [myDict writeToFile:filename atomically:YES];

// spot a pattern? :O)

原子上基本上告诉它不要覆盖任何先前的文件,直到这个文件被成功写入 - 这样如果应用程序崩溃,或者写入时存在的旧文件仍然存在。

如果您不熟悉NSDictionary和NSArray类,则值得阅读有关它们的文档。我还可以推荐一本名为iPhone 3开发的好书,这是一本很好的初学者介绍(这是我几年前开始的,我相信到现在还有4.x更新)。