繁体   English   中英

在Objective-C和NSMutableDictionary中使用条件语句执行

[英]Performance with conditional statements in Objective-C and NSMutableDictionary

基本上我的字典看起来像:

object:[NSNumber numberWithBool:] key: [NSNumber numberWithInteger:ID]

如果对象尚不存在,我想将其添加到NSMutableDictionary中。 如果两个ID相同且一个值为YES,我也想用YES覆盖任何NO值。

例如

ID:5, YES
ID:5, NO
ID:5, YES

我希望结果ID:5 YES我的字典中的ID:5 YES 我这样做:

if ([dict objectForKey:[NSNumber numberWithInteger:conditionsListID]] != nil) {
   [dict setObject:[NSNumber numberWithBool:theValue forKey:[NSNumber numberWithInteger:conditionsListID];
else {
  if (theValue) {
       [dict setObject:[NSNumber numberWithBool:theValue forKey:[NSNumber numberWithInteger:conditionsListID];

  }
}
}

我想知道是否要检查现有值是否已经为否,或者是否只要新值是否为YES就覆盖它,因为最后我想要的是。 两者之间真的有区别吗? 谢谢。

只需调用setObject:forKey:,字典就会处理您想要的逻辑。 根据API文档:

“如果字典中已经存在aKey,则会向该字典的字典的先前值对象发送释放消息,并用anObject代替。”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM