简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM