swift中三元运算符中的返回类型错误不匹配

时间:2016-05-25 23:48:48

标签: ios swift ternary-operator

我在Swift中写了一个三元语句,在语句的两种情况下我都将tableview单元格的属性设置为一个字符串或另一个字符串,如下所示:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("UITableViewCell", forIndexPath: indexPath)

    let item = items.allItems[indexPath.row]
    cell.textLabel?.text = item.name
    indexPath.row == items.allItems.count ? cell.detailTextLabel?.text = "" : cell.detailTextLabel?.text = "$\(item.valueInDollars)"
    return cell
}

但是我最终得到了这个错误:"结果值在'? :'表达式有不匹配的类型'()?'和' String?'"

我不明白为什么这里的类型不匹配 - 我在声明的两种情况下都做了完全相同的事情。有人可以对此有所了解吗?谢谢

0 个答案:

没有答案
相关问题