Swift 编译器错误 : Type '[AnyHashable : Any]?' has no subscript members

标签 swift xcode moltin

我目前正在使用 Swift 4 开发电子商务应用程序并将数据与 Moltin 关联起来。我正在为学校做这件事,所以我是新来的。我也在关注 CodeWithChris 的教程,但那已经过时并且我的布局有点不同。我按照上面的所有内容进行操作,但出现此错误:

Type '[AnyHashable : Any]?' has no subscript members

我不明白为什么。

self.objects = responseDictionary["result"] as? [AnyObject]

我尝试将 as? 更改为 as!,但仍然没有用。

这是我的完整代码;

import UIKit
import Moltin

class TableViewController: UITableViewController {
var objects = [AnyObject]()

override func viewDidLoad() {
   super.viewDidLoad()
   Moltin.sharedInstance().setPublicID('***my store ID***')

    Moltin.sharedInstance().product.listing(withParameters: nil, success: { (responseDictionary) in
        self.objects = responseDictionary["result"] as? [AnyObject]

        self.tableView.reloadData()
    }) { (responseDictionary, error) in
        print ("Something went wrong")
    }
}

最佳答案

responseDictionary 似乎是一个可选的,需要检查是否存在:

Moltin.sharedInstance().product.listing(withParameters: nil, success: { (response) in
    guard let responseDictionary = response as? [AnyHashable : Any] else {
        print("Error: respnonse is empty")
        return
    }

    self.objects = responseDictionary["result"] as? [AnyObject]

   // ...
}

关于Swift 编译器错误 : Type '[AnyHashable : Any]?' has no subscript members,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47669340/

相关文章:

javascript - 当我尝试将电子商务应用程序上传到 Heroku 时,出现 redux 记录器错误

swift - 测试时获取资源路径

ios - NSUserDefaults 显示存储的数据

ios - 在 Swift 中使用 SPUserResizableView

iphone - iPhone 4S 上的核心蓝牙故障?

ios - 从API加载图像

ios - Swift:自定义 TableView 以保存多列数据

swift :视野周围黑色

xcode - 如何写预期的失败?

node.js - 客户认证