[英]Retrieve array of dictionaries, from a dictionary in swift
我有一個json響應,除其他外,其中包含一個字典數組。 我希望能夠將該陣列復制到現有陣列中。
我在Objective-C中做到的方式是這樣的:
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSError *error = nil;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSArray *venuesArray = responseDictionary[@"response"][@"venues"];
completion(venuesArray, error);
});
迅速必須是這樣的:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
var error: NSError?
let responseDictionary: Dictionary<String, AnyObject> = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &error) as Dictionary
let venuesArray : AnyObject[] = (responseDictionary as AnyObject).valueForKeyPath("response.venues")
completionClosure(venues: responseDictionary, error: error);
});
但這給了我以下錯誤:
'AnyObject' is not convertible to 'AnyObject[]
”
任何幫助,將不勝感激!
問題出在您聲明AnyObject[]
:錯誤(“ AnyObject”不可轉換為“ AnyObject []” )清楚地表明,沒有數組初始化。
如果初始化程序應為Array值,然后用於AnyObject[]
,請首先檢查響應值。
更換:
let venuesArray : AnyObject[] = (responseDictionary as AnyObject).valueForKeyPath("response.venues")
至
let venuesArray : AnyObject = (responseDictionary as AnyObject).valueForKeyPath("response.venues")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.