-[__ NSCFString objectForKey:]:无法识别的选择器发送到实例问题 [英] -[__NSCFString objectForKey:]: unrecognized selector sent to instance issue

查看:494
本文介绍了-[__ NSCFString objectForKey:]:无法识别的选择器发送到实例问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试解析JSON时出现错误,我不理解问题...我想从XML中获取带有"objectForKey"的值.

I'm getting an error when i'm trying to parse a JSON and i don't understand the problem... I want to get a value from my XML with an "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
{

}

我在其中一种视图中调用此解析器:

I'm calling this parser in one of my view :

- (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

感谢您的帮助!

推荐答案

错误消息

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

表示您正在尝试将objectForKey:消息发送到NSString的对象-大概您实际上是想将该消息发送到NSDictionary.您确定您的JSON输入是按照您认为的方式构造的吗?

indicates that you’re trying to send the objectForKey: message to an object that is an NSString—presumably you actually wanted to send that message to an NSDictionary. Are you sure that your JSON input is structured the way you think it is?

这篇关于-[__ NSCFString objectForKey:]:无法识别的选择器发送到实例问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆