1

I am attempting to get a thumbnail url using _valueForKeyPath_ but my json has a dictionary with the path and extension separated. I don't know how to have both path and extension together within valueForKeyPath. Any help?

I tried taking a look at this, but I believe it doesn't solve my issue. Here is how my code looks like thus far:

+ (instancetype) characterWithDictionary: (NSDictionary *) dictionary {
    MCUCharacter *character = [[MCUCharacter alloc] init];

    if  ( character ) {

        character.characterImageURL = [NSURL URLWithString:[dictionary valueForKeyPath:@"thumbnail.path.extension"]];
        NSLog(@"Character Image URL: %@", character.characterImageURL);
    }

    return character;
}

And here is the json :

"thumbnail": {
      "path": "http://i.annihil.us/u/prod/marvel/i/mg/c/e0/535fecbbb9784",
      "extension": "jpg"
}, code here
Rob
  • 415,655
  • 72
  • 787
  • 1,044
Max
  • 173
  • 2
  • 8
  • something like that could / would / should do the job for you: `NSURL *_url = [NSURL URLWithString:[[dictionary valueForKeyPath:@"thumbnail.path"] stringByAppendingPathExtension:[dictionary valueForKeyPath:@"thumbnail.extension"]]];` – holex Jan 23 '18 at 16:39
  • @holex That worked! Please add an answer so I can accept it. Thank you – Max Jan 23 '18 at 16:45
  • you are welcome, I have added that as an answer. – holex Jan 23 '18 at 16:51

1 Answers1

2

I don't want to over-explain but you can grab one value-for-key-path at the time, so something like that could / would / should do the job for you:

NSURL *_url = [NSURL URLWithString:[[dictionary valueForKeyPath:@"thumbnail.path"] stringByAppendingPathExtension:[dictionary valueForKeyPath:@"thumbnail.extension"]]];

briefly, first it grabs the path then appends the extension as an actual path-extension to the string, and finally it transforms the string into an NSURL object for further use.

holex
  • 23,961
  • 7
  • 62
  • 76