d0xi45
d0xi45

Reputation: 155

Getting count of a Core Data entity's attributes

I have a Core Data entity which has logo as one of its attributes - I need to check the count of logo, so I can properly set an image view in the cell (i.e. avoid a crash when a new company is added and doesn't have a logo). With a hardcoded array of, say, logos, it's as simple as logos.count, but I'm not sure how to perform the same check on a Core Data entity. What's the best way of doing this?

    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

Upvotes: 0

Views: 290

Answers (1)

Michael Fourre
Michael Fourre

Reputation: 3015

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.

Upvotes: 1

Related Questions