【发布时间】:2014-01-24 19:44:07
【问题描述】:
我有一个 NSArray 的 UISwitches。我分别有一个 NSDictionary,其键是 NSNumber,其对象是 NSString 对象形式的 BOOL 值。我想做的是遍历 UISwitches 的 NSArray,检查标签值是否是 NSDictionary 内部的键之一,如果找到匹配项,则将 UISwitch 的 enabled 属性设置为键的对应对象(在将其从 NSString 转换为 BOOL 之后)。
我的代码如下:
for (int i=0; i<[self.switchCollection count]; i++) {
UISwitch *mySwitch = (UISwitch *)[self.switchCollection objectAtIndex:i];
if (tireSwitch.tag == //this has to match the key at index i) {
BOOL enabledValue = [[self.myDictionary objectForKey:[NSNumber numberWithInt://this is the key that is pulled from the line above]] boolValue];
mySwitch.enabled = enabledValue;
}
}
【问题讨论】:
-
您遇到了什么问题?
-
我不知道如何从与我的 for 循环中的索引 i 对应的字典中获取键。
-
嗯?您的代码已经这样做了:
[self.myDictionary objectForKey:[NSNumber numberWithInt:i]] -
检索key对应的对象。我需要密钥本身(与索引 i 不同)。然后我需要使用我得到的密钥,然后检索正确的对象。
-
给我们举个例子,说明字典里面有什么,数组里面有什么,以及你想要它们之间的什么“连接”
标签: ios objective-c nsdictionary fast-enumeration