2016-09-16 77 views
1
Alamofire.request(videosUrl!).responseJSON { response in 
     if response.result.value != nil { 

      let json = response.data 

      do { 

       let readableJSON = try JSONSerialization.jsonObject(with: json!, options: .mutableContainers) as? JSONStandard 

       if let items = readableJSON { 

        for i in 0..<items.count { 

         let item = items[i] <<< ERROR HERE 

         ...REST OF CODE HERE 
        } 
       }  

      } catch { 
       print(error) 
      } 
     } 
    } 

我也有一个typealias集:暧昧参考成员在标Alamofire

typealias JSONStandard = [String:AnyObject] 

我的问题是,为什么我收到了“暧昧参考成员标”的错误?我想要做的就是访问数组的正确部分。

+0

我下面从[这里](https://www.youtube.com/watch?v=FjsxG07haJI)到l etter是否适合我的需求,对他有用?几乎完整的代码见21:27。 –

回答

0

该错误发生时,由于Dictionary标方法(它是你JSONStandard)要求:

1)String作为关键参数,但通过i具有Int类型。并根据您的代码是基于你的例子大概应该

if let tracks = readableJSON["tracks"] as? JSONStandard { 
     if let items = tracks["items"] as? [JSONStandard] { 
      ... 
     } 
} 

更换

if let items = readableJSON { 
... 
} 

在这种情况下items成为它使用标方法Int指数的Array

2)或因为Dictionary也是索引类型为DictionaryIndex<Key, Value>下标的集合也可以预期为DictionaryIndex<String, AnyObject>。你的情况,你可以更换

for i in 0..<items.count { 

for i in items.indices { 

甚至与

for (_, item) in items { 

你选择的解决方案取决于您JSON的结构