【发布时间】:2016-04-18 08:20:09
【问题描述】:
我有一组对象,我需要在其中找到特定键的总和。
我在对象中有以下属性
class Due: NSObject {
var dueIndex: Int?
var dueAmount: Double?
}
我有以下逻辑将对象添加到数组中
var duesArray = [Due]()
for i in 0..<10 {
let dueObject = NDDue();
// Update the due object with the required data.
dueObject.dueIndex = i
dueObject.dueAmount = 100 * i
// Add the due object to dues array.
duesArray.append(dueObject)
}
在此之后,我需要对 DuesArray 中的所有值求和,以获得关键的 DueAmount。请告诉我如何使用 KVC 实现它。
我已尝试使用以下行。
print((duesArray as AnyObject).valueForKeyPath("@sum.dueAmount")).
出现以下错误
failed: caught "NSUnknownKeyException", "[Due 0x7ff9094505d0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key dueAmount."
【问题讨论】:
标签: ios arrays swift nsarray key-value-coding