首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不明确地引用了成员“”subscript“”

不明确地引用了成员“”subscript“”
EN

Stack Overflow用户
提问于 2016-10-11 22:56:21
回答 1查看 429关注 0票数 0

这是我的Swift 2代码:

代码语言:javascript
复制
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell: UITableViewCell!
    cell = tableView.dequeueReusableCellwithIdentifier: forWithIdentifier("idCellChannel", forIndexPath: indexPath as IndexPath)

    let channelTitleLabel = cell.viewWithTag(10) as! UILabel
    let thumbnailImageView = cell.viewWithTag(12) as! UIImageView

    let channelDetails = channelsDataArray[indexPath.row]
    channelTitleLabel.text = channelDetails["title"] as? String

    // Error  Ambiguous reference to member 'subscript'
    thumbnailImageView.image = UIImage(data: NSData(contentsOfURL: NSURL(string: (channelDetails["thumbnail"] as? String)!)!)!)

    return cell
}

请给我一个Swift 3的解决方案。

EN

回答 1

Stack Overflow用户

发布于 2016-10-11 23:09:01

你需要告诉编译器channelsDataArray[indexPath.row]是一个Dictionary,这样你才能用subscript。试试下面的代码:

代码语言:javascript
复制
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let channelsDataArray = [[String: AnyObject]]()//This is your array of dictionaries
    let cell = tableView.dequeueReusableCell(withIdentifier: "idCellChannel", for: indexPath) as! UITableViewCell

    let channelTitleLabel = cell.viewWithTag(10) as! UILabel
    let thumbnailImageView = cell.viewWithTag(12) as! UIImageView
    let channelDetails = channelsDataArray[indexPath.row]
    channelTitleLabel.text = channelDetails["title"] as? String
    thumbnailImageView.image = UIImage(data: NSData(contentsOf: NSURL(string: (channelDetails["thumbnail"] as? String)!)! as URL)! as Data)

    return cell
}

还要检查nil,因为你在大多数地方强制解包,这会让你的应用崩溃。使用guard letif let%s。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39980243

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档