- [__ NSCFString objectForKey:]:发送到实例问题的无法识别的选择器

时间:2014-02-17 15:03:18

标签: objective-c json parsing

当我尝试解析JSON并且我不理解问题时,我收到错误...我想从我的XML中获取一个带有“objectForKey”的值。

我的Json解析器:

@implementation contents

NSDictionary* loadedContent = nil;

+ (NSDictionary*)getContent:(NSString *)nameInnov
{
    if (loadedContent == nil) {
        NSError* error = nil;
        NSString* path = [[NSBundle mainBundle] pathForResource:@"res/contents.json" ofType:nil inDirectory:nil];
        loadedContent = [NSJSONSerialization JSONObjectWithData:[[NSFileManager defaultManager] contentsAtPath:path] options:kNilOptions error:&error];
        if (error) {
            NSLog(@"Error while parsing: %@", [error localizedDescription]);
        }
    }
    for (NSString* place in [loadedContent allKeys]) {
        NSDictionary* contents = [loadedContent objectForKey:place];
        for (NSString* key in [contents allKeys]) {
            NSDictionary* info = [contents objectForKey:key];
            if ([[info objectForKey:@"innov"] isEqualToString:nameInnov] == YES) {
                return info;
            }
        }
    }
    return nil;
}

- (void)parser:(NSXMLParser*)parser didStartElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qName attributes:(NSDictionary*)attributeDict
{

}

我在我的一个视图中调用此解析器:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [LoginModel setOFFLandingPage];

    NSDictionary* contentView = [contents getContent:@"wonderbra"];

    //self.nameInnovTextField.text = [info objectForKey:@"titre"];
}

我得到了这个错误:

2014-02-17 15:56:45.206 App[651:60b] -[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x1559a450
2014-02-17 15:56:45.208 App[651:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x1559a450'
*** First throw call stack:
(0x2edc3f4b 0x396656af 0x2edc78e7 0x2edc61cb 0x2ed154d8 0xbf945 0xb6fab 0x3153b37b 0x315e60f1 0x315e6007 0x315e55e3 0x315e530d 0x315e507d 0x315e5015 0x31536da3 0x311bdc6b 0x311b947b 0x311b930d 0x311b8d1f 0x311b8b2f 0x311b285d 0x2ed8f1cd 0x2ed8cb71 0x2ed8ceb3 0x2ecf7c27 0x2ecf7a0b 0x339f8283 0x3159b049 0xbe5e5 0x39b6dab7)
libc++abi.dylib: terminating with uncaught exception of type NSException

帮助你!

1 个答案:

答案 0 :(得分:0)

错误消息

-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x1559a450

表示您尝试将objectForKey:消息发送到NSString的对象 - 可能您实际上想要将该消息发送到NSDictionary。您确定JSON输入的结构是否符合您的想象?

相关问题