ios - 此类与键的键值编码不兼容
<p><p>我收到以下错误。 <code>restaurantData.itemArray</code> 包含 <code>ProductData</code> 对象的数组,我正在尝试使用 <code>id</code> 对其进行过滤,如下所示。我想知道我在实现中做错了什么。</p>
<blockquote>
<p>*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class
is not key value coding-compliant for the key id.'</p>
</blockquote>
<pre><code>+ (NSString *)menuItemForItemId:(NSString *)itemId
{
ProductData *restaurantData = ;
NSString *item = @"";
NSPredicate *predicate = ;
// the error is thrown in the following line
NSArray *filteredArray = ;
if ( > 0)
item = [(NSDictionary *) objectForKey:kItem];
return item;
}
</code></pre>
<hr/>
<p>如果需要,这是我的 <code>ProductData</code> 类。</p>
<p><strong>ProductData.m</strong></p>
<pre><code>#import "ProductData.h"
#define kTitleKey @"pName"
#define kPriceKey @"price"
#define kIdKey @"id"
@implementation ProductData
@synthesize pId, pImage, pPrice, pName, itemArray;
+(ProductData*) restaurantDataInstance {
static ProductData *restaurantDataInstance;
@synchronized(self) {
if(!restaurantDataInstance){
restaurantDataInstance = [ init];
}
}
return restaurantDataInstance;
}
- (id)init
{
if (self = ) {
if (!itemArray || !itemArray.count){
itemArray = ;
}
}
return self;
}
-(id)initWithDictionary:(NSDictionary *)aDict{
self = ;
if (self){
self.pId = ;
self.pPrice = ;
self.pName = ;
}
return self;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您的 <code>ProductData</code> 没有名为 <code>id</code> 的属性,从您的示例代码中我可以看到它具有 <code>pId</code></p>
<p>下面的行尝试访问名为 <code>id</code> 的属性,该属性不存在。</p>
<pre><code>NSPredicate *predicate = ;
</code></pre></p>
<p style="font-size: 20px;">关于ios - 此类与键的键值编码不兼容,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/43688342/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/43688342/
</a>
</p>
页:
[1]