Search code examples
iosswiftnsdecimalnumber

Swift: NSDecimalNumber .decimalNumberByAdding not working, but not failing


        var sumAmount: NSDecimalNumber = 0.00

        for yy in listItems {

            var selectedItem: NSManagedObject = listItems[idy] as! NSManagedObject

            var thisAmount = selectedItem.valueForKey("amount") as! NSDecimalNumber
            var thisDate = selectedCD.valueForKeyPath("date") as! NSDate

            if thisDate.isGreaterThanDate(NSDate()) {
                if sumAmount == 0.00 {
                    sumAmount = thisAmount
                } else {
                    sumAmount.decimalNumberByAdding(thisAmount)
                }
            }
            idy += 1
        }

I am trying to add thisAmount to sumAmount. Here is a sample of the code. I checked the initial sumAmount for a valid, non nil, setting, this seems correct. But the "add" is just not working. Both number are valid NSDecimalNumbers. Any suggestions? Thanks in advance.


Solution

  • Numbers are immutable objects. You have to assign result to variable.