1

My Nsdictionary looks like this.

{
 thread = "{
 \"uuid\": \"e9290344dee38c42782\",
 \"site_full\": \"www.independent.co.uk\",
 \"main_image\":\"http://www.independent.co.uk/incoming/article10498560.ece/binary/original/39-tea-party-ap.jpg\"
 }";
}  

[newDict valueForKeyPath:@"thread.main_image"];

I am trying to access it like that but it I can get all the other values which are outside the thread but cant the nested values in the thread.Any help is appreciated.

EDIT

      for (int i=0;i < [responseObject count];i++) {
                NSDictionary* newDict = (NSDictionary*)responseObject[i];

                    if ([(NSString*)[newDict valueForKey:@"title"]  isEqualToString: @""] || [newDict valueForKey:@"title"]== Nil)  {
                        NSLog(@"Image or Title Text is null");
                    }
                    else{
                    NewsFeed* newsData = [NewsFeed new];
                    newsData.newsSource = [newDict valueForKeyPath:@"thread.site"];
                    newsData.newsImageLink = [newDict valueForKeyPath:@"thread.main_image"];
              //these three work fine
                    newsData.newsTitle = [newDict valueForKeyPath:@"title"];
                    newsData.newsText = [newDict valueForKeyPath:@"text"];
                    newsData.newsDate = [newDict valueForKeyPath:@"published"]; 
              //
                    NSLog(@"news Source :: %@ \n and news ImageLink:: %@ \n news Title %@  \nnews Text:: %@ \n news Date::  %@  ",newsData.newsSource,newsData.newsImageLink,newsData.newsTitle,newsData.newsText,newsData.newsDate);
                    [NewsData addObject:newsData];
                    }


        }
soldiershin
  • 1,612
  • 1
  • 18
  • 29

2 Answers2

4

It looks like the object stored at the indexed subscript "thread" is not an NSDictionary as assumed, but rather it appears to be an instance of NSString.

It's subtle, but look at the printed value of the object at key "thread" in the root-level dictionary:

This is an NSDictionary with one key: "thread"
|
|         This is an NSString object, see the quotes (")?
|         |
V         |                     Notice that these other keys/values have their quotes (") escaped with backslash?
{         V                     |
 thread = "{                    V
 \"uuid\": \"e9290344dee38c42782\",
 \"site_full\": \"www.independent.co.uk\",
 \"main_image\":\"http://www.independent.co.uk/incoming/article10498560.ece/binary/original/39-tea-party-ap.jpg\"
 }";
} ^
  |
  Here is the end of the NSString, again we see quotes (")

Assuming this is indeed the problem, you'll need to convert the string to an actual dictionary, which you should be able to easily do using JSON serialization which this question/answer should help guide you to do.

Once this problem has been corrected, your use of valueForKeyPath: should be correct and should immediately start working.

Community
  • 1
  • 1
Beltalowda
  • 4,558
  • 2
  • 25
  • 33
  • 1
    Just checked it out...Indeed that was the problem and the suggested link's solution was exactly what i wanted..Thanks alot man.. – soldiershin Oct 02 '15 at 06:16
1

Try this modern objective-C code:

NSDictionary *myDict = @{@"thread" : @{@"uuid" : @"e9290344dee38c42782", @"site_full" : @"www.independent.co.uk", @"main_image" : @"http://www.independent.co.uk/incoming/article10498560.ece/binary/original/39-tea-party-ap.jpg"}};
NSLog(@"Image = %@", myDict[@"thread"][@"main_image"]);

EDIT: After OP response attaching debugger screenshot:

enter image description here

Abhinav
  • 37,684
  • 43
  • 191
  • 309
  • In debugginng po newDict[@"thread"] if i do this,It lists all the keys and its values but when i do this po myDict[@"thread"][@"main_image"]it gives this error error: warning: couldn't get cmd pointer (substituting NULL): no variable named '_cmd' found in this frame Execution was interrupted, reason: Attempted to dereference an invalid ObjC Object or send it an unrecognized selector. The process has been returned to the state before expression evaluation. – soldiershin Oct 02 '15 at 05:48
  • This works like a charm for me. See the attached screenshot from my debugger. I am using Xcode 7. – Abhinav Oct 02 '15 at 05:56
  • 2
    As Thuggish pointed out above, looks like your are not using the real dictionary and that is why you are not able to see it working. If you try with my sample dictionary, it should work. So crux here is check your incoming data & get it corrected to right dictionary. – Abhinav Oct 02 '15 at 06:09
  • yup that was the problem..thanks for the help people..:) – soldiershin Oct 02 '15 at 06:17