【发布时间】:2015-12-08 23:43:54
【问题描述】:
我有自定义对象,其中包含 json。如果我打印它,它看起来像
{
"first_name" = Name;
id = 111111;
"last_name" = LastName;
"photo_100" = "https://url.jpg";
}
此对象具有属性 .json。为了在 Objective-C 中从中提取数据,我使用了
NSLog(@"id%@", [[response.json firstObject] objectForKey:@"id"]);
但是在 Swift 中,如果我尝试
var dict = response.json
self.name = dict.firstObject.objectForKey("first_name")
self.lastName = dict.firstObject.objectForKey("last_name")
self.photoUrl = dict.firstObject.objectForKey("photo_100")
我得到编译或运行时错误。我尝试在 dict 声明中调用 firstObject 并尝试向下转换为字符串 - 一切都会导致错误。
如何正确提取数据?
UPD:对象定义
@interface VKResponse : VKObject
/// Request which caused response
@property(nonatomic, weak) VKRequest *request;
/// Json content of response. Can be array or object.
@property(nonatomic, strong) id json;
/// Model parsed from response
@property(nonatomic, strong) id parsedModel;
/// Original response string from server
@property(nonatomic, copy) NSString *responseString;
@end
【问题讨论】:
-
你遇到什么样的错误?这个
json变量是什么类型的?