Ahsan
Ahsan

Reputation: 2982

nsdictionary issue

I have the following data in a NSDictionary Variable:

{
    value = "TV-PG";
}

I was wondering how to get the value for key "value" here.

I tried:

        NSDictionary *fieldMaturityRating = [[parsedItems objectAtIndex:0] objectForKey:@"field_maturity_rating"];

        NSString *dateRelease = [fieldMaturityRating objectForKey:@"value"];

(where, fieldMaturityRating is a NSDictionary with the given value) and I get:

-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0xd9cd3f0
[10530:707] *** Terminating app due to uncaught exception: -[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0xd9cd3f0

Can anyone kindly help me ? Thanks.

Note: if I pause the execution and do a po after the 1st line of code presented here, I get the following:

(gdb) po fieldMaturityRatingNew
<__NSArrayM 0x79af250>( 
{
    value = "TV-PG";
}
)

Upvotes: 1

Views: 955

Answers (4)

RandomGuy
RandomGuy

Reputation: 127

Based on your datastructure which is a dictionary inside an array, dateRelease should be like this

NSString *dateRelease = fieldMaturityRating[0][@"value"];

Upvotes: 0

Joel Kravets
Joel Kravets

Reputation: 2471

This means your fieldMaturityRating is not actually an NSDictionary. Make sure you aren't setting it to an array somewhere in your code.

Edit:

This means your fieldMaturityRating is actually an NSArray containing an NSDictionary. If this is your intended data structure then you can access your value like so.

NSString *dateRelease = [[fieldMaturityRating objectAtIndex:0] objectForKey:@"value"];

I don't believe this is your intended data structure so you should look into why your parsedItems array returned you an NSArray instead of an NSDictionary. If you track this problem down you can stop any headaches in the future.

Upvotes: 2

Dave DeLong
Dave DeLong

Reputation: 243146

The po actually shows your issue:

(gdb) po fieldMaturityRatingNew
<__NSArrayM 0x79af250>( 
{
    value = "TV-PG";
}
)

The outer ( and ) mean that your object is actually an array.

Inside that is where the { and } denote your dictionary.

So you really want:

NSString *value = [[fieldMaturityRatingNew objectAtIndex:0] objectForKey:@"value"];

Upvotes: 4

Ignacio Inglese
Ignacio Inglese

Reputation: 2605

You're actually sending that NSDictionary message to a NSMutableArray instance.

You might want to check your code again as the objectForKey: method is right when pointing to a NSDictionary.

Upvotes: 2

Related Questions