0

我明白了

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryM metadata]: unrecognized selector sent to instance 0xd0b6250'

当我到达并执行这部分代码时:

IDTherapyMetadata* meta = [self.dataCon getMetaDataFromId:content.metadata];

dataCon 中的方法如下所示:

-(IDTherapyMetadata*)getMetaDataFromId:(NSString*)metaDataId
{
for (IDTherapyMetadata* metadata in self.parser.metadata)
{
    if([metaDataId compare:metadata.objId] == NSOrderedSame)
    {
        return metadata;
    }
}
return nil;

}

奇怪的是,如果我写

IDTherapyMetadata* meta = [self.dataCon getMetaDataFromId:[content valueForKey:@"metadata"]];

相反,它工作得很好。

但是我希望能够使用点符号并查看苹果的文档 https://developer.apple.com/library/ios/documentation/cocoa/conceptual/KeyValueCoding/Articles/BasicPrinciples.html 并没有帮助我。

为什么我会出现这种行为,我该如何解决?

任何帮助是极大的赞赏

编辑: IDTherapyContents.h 的代码:

#import <Foundation/Foundation.h>


@interface IDTherapyContents : NSObject 
@property (nonatomic, copy)NSString* urlId;
@property (nonatomic, copy)NSString* url;
@property (nonatomic, copy)NSString* type;
@property (nonatomic, copy)NSString* timestamp;
@property (nonatomic, copy)NSString* metadata;
@property (nonatomic, copy)NSString* lang;
@property (nonatomic, copy)NSString* objId;

//methods
-(id)initWithJsonDic:(NSDictionary*)jsonDic;

@end

IDTherpayMetaData.h 的代码

    #import <Foundation/Foundation.h>

#import "IDTherapyImages.h"

@interface IDTherapyMetadata : NSObject 

//Array contains 'String' objects
@property (nonatomic, strong)NSMutableArray* thumbnails;
@property (nonatomic, copy)NSString* title;
@property (nonatomic, copy)NSString* timestamp;
@property (nonatomic, copy)NSString* subCategory;
@property (nonatomic, copy)NSString* lang;
@property (nonatomic, copy)NSString* objId;
@property (nonatomic, copy)NSString* description5;
@property (nonatomic, copy)NSString* description4;
@property (nonatomic, copy)NSString* description3;
@property (nonatomic, copy)NSString* description2;
@property (nonatomic, copy)NSString* description1;
@property (nonatomic, copy)NSString* category;

//Array contains 'IDTherapyImages' objects
@property (nonatomic, strong)NSMutableArray* images;

//methods
-(id)initWithJsonDic:(NSDictionary*)jsonDic;

@end

IDTherapyProducts.h 的代码:

@interface IDTherapyProducts : NSObject 

//Array contains 'String' objects
@property (nonatomic, strong)NSMutableArray* contents;
@property (nonatomic, copy)NSString* uniqueId;
@property (nonatomic, copy)NSString* timestamp;
@property (nonatomic, copy)NSString* price;
@property (nonatomic, copy)NSString* posted;
@property (nonatomic, copy)NSString* normalPrice;
@property (nonatomic, copy)NSString* metadata;
@property (nonatomic, copy)NSString* lang;
@property (nonatomic, copy)NSString* objId;
@property (nonatomic, copy)NSString* badge;

//methods
-(id)initWithJsonDic:(NSDictionary*)jsonDic;

@end
4

1 回答 1

1

这是一个将属性与 KVC 混淆的简单案例。它们是不同的东西。

content.metadata只不过是[content metadata]. 所以你需要一个content被调用的方法,-metadata并且那个方法不存在。所以这就是抛出异常的原因。contentNSDictionary

在任何普通对象[content valueForKey: @"metadata"]上也会抛出异常,因为 KVC 会查找具有该名称的方法。但是,除非键以 a 开头,否则onNSDictionary -valueForKey:将被覆盖调用,在这种情况下,将剥离并调用。-objectForKey:@@[super valueForKey: ...]

但是我希望能够使用点符号

然后你需要一个方法NSDictionary被调用-metadata。你可以使用一个类别。

@interface NSDictionary(MyMetadataExtension)

-(id) metadata;

@end

@implementation NSDictionary(MyMetadataExtension)

-(id) metadata
{
    return [self objectForKey: @"metadata"];
}
@end
于 2013-09-25T10:58:13.173 回答