【发布时间】:2012-06-07 17:31:30
【问题描述】:
我正在尝试检索 NSDictionary 中键的值。字典已使用字符串类型的键值对初始化。问题是,我似乎无法通过调用 objectForKey 或 valueForKey 来检索值。
我能够遍历字典并打印键和值。
有人能指出我哪里出错了吗?这是我的代码...
//Set up dictionary with options
keys = [NSArray arrayWithObjects:@"red", @"blue", nil];
values = [NSArray arrayWithObjects:@"1.7", @"2.8", nil];
conversionOptions = [NSDictionary dictionaryWithObject:values
forKey:keys];
然后在选择器中的选择行上调用它
NSLog(@"... %@", [keys objectAtIndex:row]); //prints out the key
NSString *theString = [keys objectAtIndex:row]; //save it as a string
NSLog(@"The string is... %@", theString); //print it out to make sure im not going crazy
NSLog(@"the value is : %@",[conversionOptions objectForKey:theString]); // I just get NULL here
//NSLog(@"the value is : %@",[conversionOptions valueForKey:theString]); // This doesn't work either, I just get NULL
【问题讨论】:
-
在获取该键的对象之前,您应该在
NSLog中单独检查您的theString,以查看它是否实际上将其设置为[keys objectAtIndex:row]。也许row未初始化或未返回所需结果,因此密钥为nil或null。
标签: iphone objective-c arrays