无法将"NSObject?"类型的值分配给类型为"UIColor?"的值。



我试图将单元格的背景颜色设置为等于字典中的UI颜色,但我一直得到下面的错误。即使当传递这个作为一个数组它工作吗?

import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet weak var tableView: UITableView!
    let colourList = [["Color" : "Red", "colourCode" : UIColor.redColor()],
                        ["Color" : "Yellow", "colourCode" :     UIColor.yellowColor()],
                        ["Color" : "Purple", "colourCode" : UIColor.purpleColor()],
                        ["Color" : "Green", "colourCode" : UIColor.greenColor()],
                        ["Color" : "Blue", "colourCode" : UIColor.blueColor()],
                        ["Color" : "Orange", "colourCode" : UIColor.orangeColor()]]
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.tableView.dataSource = self
        self.tableView.delegate = self
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        let dict = colourList[indexPath.row]
        print(dict) // e.g ["Colour": "Red", "colourCode": UIDeviceRGBColorSpace 1 0 0 1] 
        print(dict["colourCode"]!) // e.g UIDeviceRGBColorSpace 1 0 0 1
        cell.backgroundColor = dict["colourCode"] \ ERROR - Cannot assign a value of type 'NSObject?' to a value of type 'UIColor?'
        return cell
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.colourList.count
    }
}

用as!明确地告诉它的类型是

cell.backgroundColor = dict["colourCode"] as! UIColor

对函数做如下修改。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    //Reusing the cell
    var cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier") as? UITableViewCell
    if cell == nil {
        cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CellIdentifier")
    }  
        let dict = colourList[indexPath.row] as! NSDictionary
        print(dict) \ e.g ["Colour": "Red", "colourCode": UIDeviceRGBColorSpace 1 0 0 1] 
        print(dict["colourCode"]!) \ e.g UIDeviceRGBColorSpace 1 0 0 1
        cell!.backgroundColor = dict["colourCode"] as! UIColor \ ERROR - Cannot assign a value of type 'NSObject?' to a value of type 'UIColor?'
        return cell!
    }

您可以轻松地在那里强制转换

cell.backgroundColor = dict["colourCode"] as! UIColor

然而,这个问题是由糟糕的设计引起的——你在一个字典中混合了两种类型的数据。相反,您可以使用辅助类/结构:

class MyColor {
    let name: String
    let color: UIColor
    init(name: String, withColor color: UIColor) {
        self.name = name
        self.color = color
    }
}

作为

let colorList = [MyColor(name: "Red", withColor: UIColor.redColor()), ...]

可以直接调用(不需要强制转换)

let myColor = colorList[indexPath.row]
cell.backgroundColor = myColor.color

字典是实物的替代品。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = UITableViewCell()
    let colour = colourList[indexPath.row]["colourCode"] as! UIColor
    cell.backgroundColor = colour
    return cell
}

最新更新