[英]How to add NSDictionary to NSArray to use it in tableview
NSDictionary *recipe = arrayOfPlist[i];
that I use it in a for loop 我在for循环中使用它
and in a if statement I added this dictionary to an NSArray
results= [recipe allValues];
在if语句中,我将此字典添加到
NSArray
results= [recipe allValues];
like this 像这样
and I segue it like this 我就这样猜了
destViewController.filteredContent=results;
I checked it the results array is not null but when I compile it and try to go to a PageView with selected array I'm having this exception 我检查了结果数组是否不为null,但是当我对其进行编译并尝试转到具有选定数组的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
is a key from my Dictionary which I use it to write to cell. RecipeName
是我的Dictionary中的一个键,我用它来写入单元格。
I couldn't see the problem so I came here. 我看不到问题,所以我来到了这里。
It turned out that s not my real problem. 原来那不是我真正的问题。 The crash happened at the next view controller which is a pageViewController.
崩溃发生在下一个视图控制器(即pageViewController)上。 I passed the results array and it should only have 1 elements in it but when i checked it it says the count 8= number of keys in my dictionaries.
我通过了结果数组,它应该只包含1个元素,但是当我检查它时,它说计数8 =我的词典中的键数。 It added all of the keys as a unique array element and thats where the problem happened.
它将所有键添加为唯一的数组元素,这就是问题发生的地方。 There is really not a recipeName ...
确实没有菜谱名称...
how can i fix this?? 我怎样才能解决这个问题??
<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>
the count is 8 for this reason. 由于这个原因,计数为8。 It consider each key as an array element.
它将每个键视为一个数组元素。
The error clearly says "There is no value for the key recipeName". 该错误清楚地表明“密钥的食谱名没有值”。
Just write the below code to make sure. 只需编写以下代码以确保。
NSArray *arrayOfPlist = [[NSArray alloc] initWithContentsOfFile:path];
recipe = arrayOfPlist[i];
NSString *myValue = [recipe valueForKey:@"recipeName"];
NSLog("value is %@", myValue);
Or Just try this and cross check the log 或尝试一下并交叉检查日志
for (id key in dictionary)
{
NSLog(@"key: %@, value: %@ \n", key, [dictionary objectForKey:key]);
}
Also, please check the xib file. 另外,请检查xib文件。 verify that, there is no "recipeName" linked to your implementation file as IBOutlet.
确认没有作为IBOutlet链接到您的实现文件的“ recipeName”。
To fix your error, change 要解决您的错误,请更改
results = [recipe allValues];
to: 至:
results = @[recipe];
<__NSCFString XxXXXXXXXXX> valueForUndefinedKey:]: this class is not key value coding-compliant for the key ...
<__ NSCFString XxXXXXXXXXX> valueForUndefinedKey:]:此类不符合密钥的键值编码标准...
The error tells us: 该错误告诉我们:
valueForUndefinedKey:
( due to a -valueForKey:
) valueForUndefinedKey:
由于-valueForKey:
-valueForKey:
-valueForKey:
For -valueForKey:
to work, NSString
will reference a value from a key ( which indicates that a specified key should have a related value ). 为了使
-valueForKey:
有效, NSString
将引用键中的值( 指示指定的键应具有相关值 )。
But in your case, results = [recipe allValues];
但在您的情况下,
results = [recipe allValues];
has returned you an array of only strings and not key-value pairs ( ie not a dictionary ) and hence valueForKey:@"recipeName"
will cause an error since the recipeName
is taken as a key and it does not have a related value. 已经返回了一个只有字符串而不是键值对( 即不是字典 )的数组,因此
valueForKey:@"recipeName"
会导致错误,因为recipeName
被当作键,并且没有相关值。
//this will not cause the error
NSDictionary *dctTest = @{@"recipeName": @"Pancakes"};
NSString *strSuccess = [dctTest valueForKey:@"recipeName"];
//this will cause the error
NSArray *arrTest = @[@"Pancakes"];
NSString *strError = [arrTest valueForKey:@"recipeName"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.