【问题标题】:-[__NSCFString objectForKey:]: unrecognized selector sent to instance issue-[__NSCFString objectForKey:]:无法识别的选择器发送到实例问题
【发布时间】:2023-03-14 18:28:01
【问题描述】:

我在尝试解析 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

感谢您的帮助!

【问题讨论】:

    标签: objective-c json parsing


    【解决方案1】:

    错误信息

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

    表示您正在尝试将objectForKey: 消息发送给NSString 的对象——大概您实际上想将该消息发送给NSDictionary。您确定您的 JSON 输入的结构与您认为的一样吗?

    【讨论】:

    • 其实是我的JSON结构。我将解析器的“For Loop”更改为: for (NSString* place in [loadedContent allKeys]) { NSDictionary* info = [loadedContent objectForKey:place]; if ([[info objectForKey:@"innov"] isEqualToString:nameInnov] == YES) { 返回信息; } } 谢谢!!
    猜你喜欢
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多