Objective c 帮助使用字典和plist

Objective c 帮助使用字典和plist,objective-c,plist,nsdictionary,Objective C,Plist,Nsdictionary,我有一个.plist文件,其中包含一个字典项,如下所示: <dict> <dict> <key>Key one</key> <string>string</string> ... </dict> ... </dict>

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

<dict>
          <dict>
                     <key>Key one</key>
                     <string>string</string>
                     ...
          </dict>
          ...
</dict>
编辑。 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>

关键一
关键一
一串
...
关键二
关键一
一串
...

我不是100%确定我知道你在找什么,你是说你想编辑密钥字符串,还是要编辑存储在ket中的字符串,我怀疑是后者

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需要声明为可变的,因为根字典本身没有改变,只有子字典

当然,在继续修改结果之前,您应该验证从[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的书中读到,在Objective C中,仅仅测试指针是不够的:

if(子词典) { }


正如在布尔测试中一样,目标C只测试底部8位的非零值,并忽略指针中的其余位,因此,如果指针位于256字节边界上,if将返回false,而实际上为true。这可能在Objective-C 2.0中已经解决,但是为了安全起见,我总是使用长柄版本。

不100%确定我知道你在做什么,你的意思是要编辑密钥字符串,还是要编辑存储在ket中的字符串,我怀疑后者

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需要声明为可变的,因为根字典本身没有改变,只有子字典

当然,在继续修改结果之前,您应该验证从[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的书中读到,在Objective C中,仅仅测试指针是不够的:

if(子词典) { }


正如在布尔测试中一样,目标C只测试底部8位的非零值,并忽略指针中的其余位,因此,如果指针位于256字节边界上,if将返回false,而实际上为true。这可能在Objective-C2.0中已经解决了,但是为了安全起见,我总是使用长柄版本。

保存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类,那么值得仔细阅读它们的文档。我还可以推荐一本名为《Apress开发iPhone 3》的好书,这是一本很好的初学者入门书(这是我几年前开始的书,我相信现在已经有了4.x更新)。

保存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类,那么值得仔细阅读它们的文档。我还可以推荐一本名为《Apress开发iPhone 3》的好书,这是一本很好的初学者入门书(这是我几年前开始的书,我相信现在已经有4.x更新了)。

我忘了指定一件事,plist是这样的(对不起):好的,还是不太确定你在问什么。我现在知道你有一个plist,包含一个字典,其中包含另外两个字典,每个字典中都有一个字符串/键,你想编辑这两个字典,对吗?我忘了指定一件事,plist是这样的(对不起):好的,仍然不太确定你在问什么。我现在知道你有一个plist,包含一个字典,其中包含另外两个字典,每个字典中都有一个字符串/键,你想编辑这两个字典,对吗?