如何检查plist中是否存在键?

raz*_*iiq 0 iphone xcode

我试图检查xcode中plist文件中是否存在密钥.我的plist文件有这种结构.

Root (Dictionary)
+- Parent1 (Dictionary)
   - Key1 (Boolean)
   - Key2 (Boolean)
   - Key3 (Boolean)
   - Key4 (Boolean)

+- Parent2 (Dictionary)
   - Key1 (Boolean)
   - Key2 (Boolean)
Run Code Online (Sandbox Code Playgroud)

现在我需要检查在Parent1中是否存在Key2?我检查了NSDictionary,但无法得到如何做到这一点.

有关如何做到这一点的任何建议?

zou*_*oul 5

 NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:@"your.plist"];
 BOOL key2Exists = [[dict objectForKey:@"Parent1"] objectForKey:@"Key2"] != nil;
Run Code Online (Sandbox Code Playgroud)

至于明确的nil比较,我有时是因为它使代码更易读,我(它提醒我,在语句的左侧的变量是一个布尔值)使用它.我也看过一个明确的"布尔演员":

BOOL key2Exists = !![[dict objectForKey:@"Parent1"] objectForKey:@"Key2"];
Run Code Online (Sandbox Code Playgroud)

我想这是个人喜好的问题.