我使用以下代码迭代plist来查找字符串 然后我想使用键的值 - 分数和数字 不幸的是有些东西不起作用,我无法弄清楚
任何帮助将不胜感激
NSString *theNumber = @"0.0075";
BOOL found = NO;
NSUInteger f;
for (f = 0; f < [selectorKeysFractions count]; f++) {
NSString * stringFromArray = [selectorKeysFractions objectAtIndex:f];
if ([theNumber isEqualToString:stringFromArray]) {
found = YES;
break;
}
}
if ( found ) {
// do found
//this line fails with unrecognised selector message
NSLog(@"%@",[[selectorKeysFractions objectAtIndex:f]objectForKey:@"fraction"]);
} else {
// do not found
}
我的plist数据看起来像这样
<plist version="1.0">
<dict>
<key>fraction</key>
<string>1/128</string>
<key>number</key>
<string>23.78</string>
</dict>
<dict>
<key>fraction</key>
<string>1/234</string>
<key>number</key>
<string>25</string>
</dict>
</plist>
我正在设置像这样的数组
NSString *path2 = [[NSBundle mainBundle] pathForResource:@"Fractions" ofType:@"plist"];
pickerData2 =[[NSDictionary alloc]initWithContentsOfFile:path2];
selectorKeysFractions = [[NSArray alloc] initWithArray:[pickerData2 allKeys]];
我做错了什么但是弄不清楚 请帮忙
感谢
答案 0 :(得分:0)
将消息“objectForKey”发送到NSString的对象。您必须要求您的NSDictionary才能获得正确的条目。
检查一下:
NSLog(@"%@",[pickerData2 objectForKey:[selectorKeysFractions objectAtIndex:f]]);
这应该返回你的字符串。