简体   繁体   English

输入“ [[AnyHashable:任何]”? 没有下标成员-使用Moltin API

[英]Type '[AnyHashable : Any]?' has no subscript members - Using Moltin API

I'm doing a tutorial from 2015 and very new to swift, I'm getting the Type [AnyHashable : Any]? 我正在[AnyHashable : Any]?的教程,并且很快就开始学习,我正在使用Type [AnyHashable : Any]? has no subscript members error from the self.objects line. self.objects行没有下标成员错误。

Moltin.sharedInstance().product.listing(withParameters: nil, success: { (responseDictionary)-> Void in
      //Assign Products array to object property
      self.objects = responseDictionary["result"] as! [AnyObject]

  }) { (responseDictionary, error) in
      print("Something went wrong!")
  }
}

Refactored your code to properly cast the response into a dictionary. 重构您的代码以将响应正确地转换为字典。

Moltin.sharedInstance().product.listing(withParameters: nil, success: { (response) -> Void in
    //Assign Products array to object property

    guard let responseDictionary = response as? [String: AnyObject] else {
        return
    }

    self.objects = responseDictionary["result"] as! [AnyObject]
    print(self.objects)
    //Tell the table view to reload it's data
    self.tableView.reloadData()
}) { (responseDictionary, error) in
    print("Something went wrong!")
}

I tried running your code after this, it works alright. 我尝试在此之后运行您的代码,一切正常。 The API isn't returning what you expect. API没有返回您期望的结果。 Let's take a look... 让我们来看看...

let pagination = responseDictionary["pagination"]!
let results = responseDictionary["result"] as! [AnyObject]

print(pagination.count) // 8
print(results.count) // 0

As you can see there are 8 value under the pagination key but 0 under the results key. 如您所见, pagination键下有8个值,而results键下有0个值。 Which means that the parsing logic is correct. 这意味着解析逻辑是正确的。 If there is an error it's on the API end. 如果有错误,则在API端。 The API doesn't return any results, that's why the table is empty. API不返回任何结果,这就是表为空的原因。

Hard luck, I hope this helps though! 祝您好运,但希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM