0

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".

My Json parser :

@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"];
}

And i get this error :

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

Thx for helping !

1 Answer 1

0

The error message

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

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?

Sign up to request clarification or add additional context in comments.

1 Comment

In fact, it was my JSON structure. I change the "For Loop" of my parser to : for (NSString* place in [loadedContent allKeys]) { NSDictionary* info = [loadedContent objectForKey:place]; if ([[info objectForKey:@"innov"] isEqualToString:nameInnov] == YES) { return info; } } Thx!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.