简体   繁体   中英

Cannot assign value of type 'UIColor' to type 'String?'

I am building a Sidebar with 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 !

The error is at the line:

cell!.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 ).

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 . You want to use the textColor property on the cell's textLabel instead, like so:

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

Also note that you have a reusable cell identifier mismatch ( "Cell" for newly created ones, "cell" for fetching them).


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. 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 ( ?? ). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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