ios - 如何将 NSDictionary 添加到 NSArray 以在 tableview 中使用它

标签 ios objective-c uitableview

NSDictionary *recipe = arrayOfPlist[i];

我在for循环中使用它

在 if 语句中,我将这个字典添加到 NSArray results= [recipe allValues]; 就像这样

然后我这样继续

     destViewController.filteredContent=results;

我检查结果数组不为空,但是当我编译它并尝试转到具有选定数组的 PageView 时出现此异常

 *** Terminating app due to uncaught exception 'NSUnknownKeyException', 
reason: '[<__NSCFString 0x109268e40> valueForUndefinedKey:]: this class is not key value coding-compliant for the key recipeName.'

RecipeName 是我字典中的一个键,我用它来写入单元格。

我看不到问题所以我来了。


原来这不是我真正的问题。崩溃发生在下一个 View Controller ,它是一个 pageViewController。我传递了结果数组,它应该只有 1 个元素,但是当我检查它时,它说 count 8= number of keys in my dictionaries。它将所有键添加为唯一的数组元素,这就是问题发生的地方。真的没有recipeName ...

我该如何解决这个问题??

<plist version="1.0">
<array>
    <dict>
        <key>category</key>
        <string>desert</string>
        <key>numberOfPerson</key>
        <string>3</string>
        <key>recipeImage</key>
        <string>asdd.jpg</string>
        <key>time</key>
        <string>15</string>
        <key>recipeName</key>
        <string>Puding</string>
        <key>recipeDetail</key>

因此计数为 8。它将每个键视为一个数组元素。

最佳答案

错误清楚地表明“键 recipeName 没有值”。

只需编写以下代码即可确定。

NSArray *arrayOfPlist = [[NSArray alloc] initWithContentsOfFile:path]; 
recipe = arrayOfPlist[i]; 

NSString *myValue = [recipe valueForKey:@"recipeName"];
NSLog("value is %@", myValue);

或者尝试一下并交叉检查日志

for (id key in dictionary)
{
    NSLog(@"key: %@, value: %@ \n", key, [dictionary objectForKey:key]);
}

另外,请检查 xib 文件。确认没有“recipeName”作为 IBOutlet 链接到您的实现文件。

关于ios - 如何将 NSDictionary 添加到 NSArray 以在 tableview 中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23315169/

相关文章:

ios - iAd 在 iAd 的顶部和底部留下空白

iOS - UITableViewCell 中文本周围的圆角矩形,如 Things 应用程序

iphone - 无法解析以下 XML?

ios - xcode 中的 ScrollView 使用 swift 和 3 个图像层

iphone - 如何在 UINavigationController 中正确继承 UITableViewController?

ios - 最后一个单元格不断出现在 myUITableView 上

ios - 将 isTouchInside 设置为 false

ios - 给定纬度和经度生成快照的最佳方法是什么,真的鼓励跳过 MKMapView

objective-c - 使用 DidSelectRowAtIndexPath 更改 NSMutableArray

ios - Swift:将单个图像添加到 UITableView 中所有单元格的最佳方式