0

Lets say I have two Entitys. Item: name (to-many-Attribute)

Attribute: income: (to-one-Item)

Firstly, I follow this answer to get the sum.

let totalIncome = item.value(forKeyPath: "attribute.@sum.income")

But totalIncome I get is Any type, not Int as the income. How to make it work? enter image description here

Secondly, if I get the count number of the incomes, how to write the code?

Thanks for any help!

Muz
  • 699
  • 1
  • 11
  • 25

1 Answers1

2

The following should work

let totalIncome = item.value(forKeyPath: "attribute.@sum.income") as? Int ?? 0
Asperi
  • 228,894
  • 20
  • 464
  • 690