ios - 条件绑定(bind)中的绑定(bind)值必须是可选类型 [IOS - SWIFT]

标签 ios swift

我正在尝试使用 Swift 在 Xcode 中制作一个 TO-DO 列表应用程序,但我在“if let path = indexPath {”行中编写其中一个函数方法时遇到错误,该行显示“条件中的绑定(bind)值”绑定(bind)必须是 Optional 类型”。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
    if let path = indexPath {            
        let currentString  = dataSource[path.section][path.row]
        cell.textLabel?.text = currentString            
    }        
    return cell
}

最佳答案

因为 indexpath 不是可选的,所以你不需要使用条件绑定(bind)

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
    let currentString  = dataSource[indexPath.section][indexPath.row]
    cell.textLabel?.text = currentString
    return cell
}

关于ios - 条件绑定(bind)中的绑定(bind)值必须是可选类型 [IOS - SWIFT],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31353834/

相关文章:

ios - Swift:将图像设置为自定义单元格ImageView

ios - 如何在 Swift 中为 OTP 倒计时

ios - swift 改变语言

iPhone App,服务器端组件,解析集成

ios - 为什么 SKShapeNode 在顶部边框上更亮

swift - 使用 NSAttributedString 设置字体

ios - UIView.animateWithDuration :animations:completion: gets canceled in XCTest

ios - 选择器获取indexPath UICollectionView Swift 3.0

ios - 以编程方式查找通过电缆将iOS设备连接到的MacBook的IP

swift - 如何将 SCNNode 移动到 ARCamera 正下方?