0

Hi I am trying to extract key value from NSDictionary, the dictionary value looks like below screen shot

enter image description here

I need to extract the value with key "TITLE" to NSString, using the code

NSDictionary* tmp = [self getDBRequest:req];
NSString * title =[tmp valueForKey:@"TITLE"];

But giving the the value like

enter image description here

Is there anything wrong with above code?

Edit:

NSLog(@"%@", tmp);

Gives the output

2015-12-31 11:04:52.530 SimpleTable[610:10059] (
        {
        DESCRIPTION = "30% OFF ON NEW ";
        "IMAGE_URL" = "crowd.jpg";
        TITLE = "GET 30% OFF";
    }
)

Edit2

Actualy using the result of NSString * title =[tmp valueForKey:@"TITLE"]; I have to replace an element of NSMutableArray

And the code

NSMutableArray *array = [[NSMutableArray alloc]init];
 [array addObject:@"Eezy"];
 [array addObject:@"Tutorials"];

 NSDictionary* tmp = [self getDBRequest:req];
 NSString * title =[tmp valueForKey:@"TITLE"];
[array replaceObjectAtIndex:0 withObject:title];

Giving me the array modified some thing like below screen shot

enter image description here

Haris
  • 13,645
  • 12
  • 90
  • 121
  • can you tell us, what NSLog(@"%@", tmp); prints to the console? – FelixSFD Dec 31 '15 at 15:46
  • And in what way is what you are getting not what you expected? – luk2302 Dec 31 '15 at 15:49
  • 2
    That code is working properly; although you want `objectForKey:`, not `valueForKey:`. – trojanfoe Dec 31 '15 at 15:49
  • @trojanfoe `objectForKey:` giving me an exception like `2015-12-31 11:15:11.297 SimpleTable[649:12131] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x7fd09941a240'` – Haris Dec 31 '15 at 16:18
  • @FelixSFD Please see my edited question. – Haris Dec 31 '15 at 16:18
  • 1
    @Haris That doesn't make sense; are you sure it's on that line? What you show is a dictionary, not an array. – trojanfoe Dec 31 '15 at 16:20
  • according to the output, you posted, it should work. As @trojanfoe said, this seems to be not a problem with the `NSDictionary` – FelixSFD Dec 31 '15 at 16:22
  • Hi Please see my edited question. – Haris Dec 31 '15 at 16:29
  • So now you've changed the question completely. – trojanfoe Dec 31 '15 at 16:34
  • @trojanfoe I modified the code as per suggestion `NSString * title =[tmp objectForKey:@"TITLE"];` and put the break point on same line and it gave exception when click stepover with the error message `2015-12-31 11:30:59.836 SimpleTable[720:15138] -[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x7f926240bab0 2015-12-31 11:30:59.840 SimpleTable[720:15138] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x7f926240bab0' *** First throw call stack:` – Haris Dec 31 '15 at 16:35
  • @trojanfoe My final goal is to add/replace the value of an NSMutableArray. – Haris Dec 31 '15 at 16:37

2 Answers2

1

Look at the first image in your question, the first line of the contents dump says "1 object".

Also look at the output of the NSLog, notice the parentheses (( & )) which surround the braces ({ & }), which in turn surround the key/value pairs.

Both these are telling you that tmp is not referencing an NSDictionary as you think, but an NSArray containing a single element and that element is an NSDictionary.

Now when you invoke valueForKey: on an array of dictionaries it does the key lookup on every dictionary in the collection and returns and array of results.

Which is why when you look at the second image in your question you see that its contents dump also starts with "1 object" - title is referencing an array containing one element, being your string.

This is also why, as mentioned in the comments, that using objectForKey: in place of valueForKey: causes an error - that method does not operate on arrays and so produces the unrecognized selector sent to instance error.

HTH

jlehr
  • 15,557
  • 5
  • 43
  • 45
CRD
  • 52,522
  • 5
  • 70
  • 86
  • You are right my data was something like explained here http://stackoverflow.com/questions/26311614/convert-json-to-nsarray I changed the line `NSString * title =[tmp valueForKey:@"TITLE"];` to `NSArray *title = [tmp valueForKey:@"TITLE"];` and the issue solved. Thanks for the detailed answer. – Haris Jan 01 '16 at 06:28
-3

try like bellow code.

NSDictionary *tmp = @{@"DESCRIPTION" : @"30 OFF ON NEW",@"IMAGE_URL" : @"crowd.jpg",@"TITLE" : @"GET 30% OFF"};
//NSDictionary* tmp = [self getDBRequest:req];
NSArray *keyArray = [tmp allKeys];
NSString * title = [keyArray objectAtIndex:[keyArray indexOfObject:@"TITLE"]];//[keyArray objectAtIndex:0];
[array replaceObjectAtIndex:0 withObject:title];
Jamil
  • 2,977
  • 1
  • 13
  • 23