计算核心数据实体的属性

时间:2017-01-26 21:07:01

标签: ios swift uitableview core-data

我有一个核心数据实体,其logo作为其属性之一 - 我需要检查徽标的数量,因此我可以在单元格中正确设置图像视图(即避免在新的时候崩溃) company已添加,但没有徽标。使用硬编码数组,比如logos,它就像logos.count一样简单,但我不确定如何对核心数据实体执行相同的检查。这样做的最佳方式是什么?

    DispatchQueue.main.async {
        if /*What to count?*/.count >= indexPath.row + 1 {
            cell.logoView.image = UIImage(named: (company.value(forKey: "logo") as? String)!)
        } else {
            cell.logoView.image = UIImage(named: "noImage")
        }
    }

enter image description here

1 个答案:

答案 0 :(得分:1)

Based on what I can see from your current setup, the following should be fine:

DispatchQueue.main.async {
    if let logo = company.value(forKey: "logo") as? String {
        cell.logoView.image = UIImage(named: logo)
    } else {
        cell.logoView.image = UIImage(named: "noImage")
    }
}

Let me know if this makes sense.