【发布时间】:2018-01-23 16:32:15
【问题描述】:
我正在尝试使用 _valueForKeyPath_ 获取缩略图 url,但我的 json 有一个字典,其中路径和扩展名是分开的。我不知道如何在valueForKeyPath 中同时拥有路径和扩展名。有什么帮助吗?
我尝试查看this,但我相信它不能解决我的问题。到目前为止,我的代码如下所示:
+ (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;
}
这是json:
"thumbnail": {
"path": "http://i.annihil.us/u/prod/marvel/i/mg/c/e0/535fecbbb9784",
"extension": "jpg"
}, code here
【问题讨论】:
-
类似的东西可以/会/应该为您完成这项工作:
NSURL *_url = [NSURL URLWithString:[[dictionary valueForKeyPath:@"thumbnail.path"] stringByAppendingPathExtension:[dictionary valueForKeyPath:@"thumbnail.extension"]]]; -
@holex 成功了!请添加答案,以便我接受。谢谢
-
不客气,我已将其添加为答案。
标签: ios objective-c json key-value-observing