Reputation:
My code right now calls one core data attribute "lorde" and places it on a tableview cell. I want the cell to display both "lorde" and other attribute "num". I want both attributes to be printed on
cell.textLabel?.text.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let title = itemsName[indexPath.row]
let cell = tazbleView.dequeueReusableCell(withIdentifier: "cell", for : indexPath)
cell.textLabel?.text = title.value(forKey: "lorde") as? String
return cell
}
Upvotes: 0
Views: 63
Reputation: 1206
I prefer this way It looks more nice and not stop of using when just one attribute exists
let attr1 = title.value(forKey: "lorde") as? String
let attr2 = title.value(forKey: "num") as? String
cell.textLabel?.text = [attr2, attr1].flatMap { $0 }.reduce("", +)
Upvotes: 1
Reputation: 5729
You can concatenate the two strings with the +
operator.
guard let lorde = title.value(forKey: "lorde"),
let num = title.value(forKey: "num")
else { fatalError("cannot unwrap title keys") }
cell.textLabel?.text = lorde + num
Upvotes: 0