简体   繁体   English

无法将类型“ UIColor”的值分配为类型“ String?”

[英]Cannot assign value of type 'UIColor' to type 'String?'

I am building a Sidebar with Swift. 我正在使用Swift构建补充工具栏。 And I got this error in this code: 我在此代码中收到此错误:

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

    if cell == nil{
        cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")

        cell!.backgroundColor = UIColor.clearColor()
        cell!.textLabel!.text = UIColor.darkTextColor()
    }

    // Configure the cell...

    return cell
}

So there actually is someone on this platform that has the same problem. 因此,实际上这个平台上有人存在相同的问题。 In the solution they said I have to add a ! 在解决方案中,他们说我必须添加一个! behind UIColor.darkTextColor() but if I do that there is another error that I have to delete ! UIColor.darkTextColor()后面,但是如果我这样做,则必须删除另一个错误!

The error is at the line: 错误在一行:

cell!.textLabel! 单元!.textLabel!

Do you guys know what's going on? 你们知道发生了什么吗?

The error is due to this code: 该错误是由于以下代码引起的:

cell!.textLabel!.text = UIColor.darkTextColor()

You are assigning UIColor to a property that expects to be a String ( text property of UILabel ). 您正在将UIColor分配给期望为String属性( UILabel text属性)。

I think you are probably looking to change the text color , if so you need to change the code like: 我认为您可能正在寻找更改文本颜色的方法 ,如果需要,您需要更改以下代码:

cell!.textLabel!.textColor = UIColor.darkTextColor()

The problem is you're trying to assign a UIColor to a String . 问题是您正在尝试将UIColor分配给String You want to use the textColor property on the cell's textLabel instead, like so: 您想改为使用textColor格的textLabel上的textColor属性,如下所示:

cell.textLabel?.textColor = UIColor.darkTextColor()

Also note that you have a reusable cell identifier mismatch ( "Cell" for newly created ones, "cell" for fetching them). 还要注意,您有一个可重用的单元标识符不匹配( "Cell"代表新创建的标识符, "cell"代表获取它们)。


However, there are bigger issues here. 但是,这里有更大的问题。

You really shouldn't be littering about crash operators ( ! ) in order to dismiss compiler errors. 为了消除编译器错误,您真的不应该乱扔崩溃操作符! )。 Sure, it may be completely 'safe' now (as you do an == nil check) – but it just encourages the future use of them in places where they really shouldn't ever be used. 当然,现在可能已经完全“安全”了(因为您进行了== nil检查)–但它只是鼓励将来在不应该使用的地方使用它们。 They can also be pretty dangerous for future code refactorings. 它们对于将来的代码重构也可能非常危险。

I would recommend you re-write your code to take advantage of the nil-coalescing operator ( ?? ). 我建议您重新编写代码,以利用nil-coalescing运算符( ?? )。 You can use this to attempt to get a re-useable cell. 您可以使用此方法尝试获取可重复使用的单元格。 If that fails, then you can substitute in a newly created one instead. 如果失败,则可以替换为新创建的一个。 You can also use an auto-executing closure ( {...}() ) in order to do some common cell setup. 您也可以使用自动执行的闭包( {...}() )进行一些常见的单元设置。

For example: 例如:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    // attempt to get a reusable cell – create one otherwise
    let cell = tableView.dequeueReusableCellWithIdentifier("foo") ?? {

        // create new cell
        let cell =  UITableViewCell(style: .Default, reuseIdentifier: "foo")

        // do setup for common properties
        cell.backgroundColor = UIColor.redColor()
        cell.selectionStyle = .None

        // assign the newly created cell to the cell property in the parent scope
        return cell
    }()

    // do setup for individual cells
    if indexPath.row % 2 == 0 {
        cell.textLabel?.text = "foo"
        cell.textLabel?.textColor = UIColor.blueColor()
    } else {
        cell.textLabel?.text = "bar"
        cell.textLabel?.textColor = UIColor.greenColor()
    }

    return cell
}

Now it's easy to spot whether a ! 现在很容易发现是否! belongs in your code or not. 是否属于您的代码。 It ... er doesn't. 没错。

Don't trust anyone who recommends adding extra crash operators to your code in order to solve your problems. 不要相信建议您在代码中添加额外的崩溃运算符以解决问题的人。 It just becomes a source of more problems. 它只是成为更多问题的根源。

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

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