2015-12-13 61 views
1

我嘗試使用Sum聚合函數上的屬性,但我得到一個奇怪的錯誤,我是無法糾正:使用金額合計與CoreData

2015-12-13 11:32:29.280 YoBuM[634:42509] -[NSDecimalNumber count]: unrecognized selector sent to instance 0x618000025ce0 2015-12-13 11:32:29.281 YoBuM[634:42509] -[NSDecimalNumber count]: unrecognized selector sent to instance 0x618000025ce0 2015-12-13 11:32:29.286 YoBuM[634:42509] ( 0 CoreFoundation 0x00007fff8a8e0ae2 __exceptionPreprocess + 178 1 libobjc.A.dylib 0x00007fff97b6473c objc_exception_throw + 48 2 CoreFoundation 0x00007fff8a8e3b9d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 3 CoreFoundation 0x00007fff8a81c601 ___forwarding___ + 1009 4 CoreFoundation 0x00007fff8a81c188 _CF_forwarding_prep_0 + 120 5 Foundation 0x00007fff9796cff6 +[_NSPredicateUtilities _getCommonTypeFor:] + 76 6 Foundation 0x00007fff9796d30a +[_NSPredicateUtilities sum:] + 144 7 Foundation 0x00007fff977d2cff -[NSFunctionExpression expressionValueWithObject:context:] + 1094 ...

我的代碼是:

import Foundation 
import CoreData 

@objc(Operation) 
class Operation: NSManagedObject { 
    @NSManaged var amount: NSNumber? 
    @NSManaged var date: NSDate? 
    @NSManaged var days: NSNumber? 
    @NSManaged var designation: String? 
    @NSManaged var isDebit: NSNumber? 
    @NSManaged var element: Element? 

    class func tests(appDel: AppDelegate, forElement: Element) -> (q1: Double, q2: Double, q3: Double, q4: Double) { 
     let moc = appDel.managedObjectContext 
     let request = NSFetchRequest(entityName: "Operation") 

     let expressionDesc = NSExpressionDescription() 
     expressionDesc.name = "sumOfAmount" 
     let amountExpr = NSExpression(forKeyPath: "amount") 
     expressionDesc.expression = NSExpression(forFunction: "sum:", arguments: [amountExpr]) 
     expressionDesc.expressionResultType = .DoubleAttributeType 
     request.propertiesToFetch = [expressionDesc] 
     request.resultType = .DictionaryResultType 

     let elementPredicate = NSPredicate(format: "element = %@", forElement) 
     let creditPredicate = NSPredicate(format: "isDebit = %@", true) 

     let year = (forElement.budget as! Budget).year!.integerValue 
     var minDate = Utils.getDate("01/01/\(year)") 
     var maxDate = Utils.getDate("04/01/\(year)") 
     let datesPredicate = NSPredicate(format: "(date >= %@) AND (date < %@)", minDate, maxDate) 
     let predicates = NSCompoundPredicate(andPredicateWithSubpredicates: [elementPredicate, creditPredicate, datesPredicate]) 
     request.predicate = predicates 

     do { 
      //error is here when some entities are found for the given predicates 
      let results = try moc.executeFetchRequest(request) 

      // ... 
     } 
     catch { 
      fatalError("Oops: \(error).") 
     } 
     //return (q1, q2, q3, q4) 
    } 
} 

我不知道爲什麼在錯誤跟蹤中有一個計數選擇器。 我在這裏選擇「好」的方法來求和嗎?

謝謝。

回答

0

首先,這應該是一個類方法。您正在獲取任意Operation實例,因此沒有很好的理由說明爲什麼應該在一個特定實例的上下文中調用它。

其次,我認爲這可以大大如下簡化:獲取所有操作與給定的約束條件,則:

let q1Sum = (operations as NSArray).valueForKeyPath("@sum.amount").doubleValue 

,或者更「迅速」:

let q1Sum = operations.map { $0.amount.doubleValue }.reduce(0, combine: +) 

沒有除非您遇到性能問題,否則很可能會出現NSExpression複雜性和使用NSDictionaryResultType的明顯原因。

+0

好的,謝謝你的關注。 但我不明白'操作'的起源。 如果我評論所有關於NSExpression的行,'results'將包含一個'Operation'數組,我不知道如何聚合。我應該錯過一些明顯的... – Yoryll

+0

你會用適當的謂詞過濾器來獲取操作。總和的計算顯示在我的答案中。 – Mundi

+0

對不起,我對這個問題的可憐的知識,我開始與斯威夫特,所以我不明白;) 我刪除了所有關於「NSExpressions」,並改變了這樣的獲取請求:'let q1Sum = results.valuesForKeyPath )// - >類型'[Operation]'的值沒有成員'valuesForKeyPath'。換句話說,我怎麼得到這個'操作'變量/成員,讓我調用valuesForKeyPath(字符串)? – Yoryll