Objective C 连接键
Objective C concatenation keys
我正在尝试使用 _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
我不想过度解释,但你可以在当时抓取一个 value-for-key-path,所以类似的事情可以/将/应该做适合你的工作:
NSURL *_url = [NSURL URLWithString:[[dictionary valueForKeyPath:@"thumbnail.path"] stringByAppendingPathExtension:[dictionary valueForKeyPath:@"thumbnail.extension"]]];
简而言之,首先它获取 path 然后附加 extension 作为字符串的实际路径扩展,最后它转换将字符串转换为 NSURL
对象以供进一步使用。
我正在尝试使用 _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
我不想过度解释,但你可以在当时抓取一个 value-for-key-path,所以类似的事情可以/将/应该做适合你的工作:
NSURL *_url = [NSURL URLWithString:[[dictionary valueForKeyPath:@"thumbnail.path"] stringByAppendingPathExtension:[dictionary valueForKeyPath:@"thumbnail.extension"]]];
简而言之,首先它获取 path 然后附加 extension 作为字符串的实际路径扩展,最后它转换将字符串转换为 NSURL
对象以供进一步使用。