我是php web开发者。我最近学会了ios swift。我试图从核心数据中获取记录,并根据ios swift中的一个属性根据一列或更多列适当地对记录进行分组。我已经尝试过Stack Overflow的解决方案,但它们都不适用于我。所以请有人指出我做错了什么。这是我的代码:
var context : NSManagedObjectContext = appdel.managedObjectContext!
var request = NSFetchRequest(entityName: "TblOrders")
request.returnsObjectsAsFaults = false
request.propertiesToGroupBy = ["order_num"]
request.propertiesToFetch = ["cust_name", "brand"]
request.resultType = .DictionaryResultType
context.executeFetchRequest(request, error: nil)!
var results = context.executeRequest(request, error: nil)!
println(results)
这是错误消息:
2015-08-11 10:54:30.423 newapp[470:6013] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'SELECT clauses in queries with GROUP BY components can only contain properties named in the GROUP BY or aggregate functions ((<NSAttributeDescription: 0x7fa8ca5b4b70>), name cust_name, isOptional 1, isTransient 0, entity TblOrders, renamingIdentifier cust_name, validation predicates (
), warnings (
), versionHashModifier (null)
userInfo {
}, attributeType 700 , attributeValueClassName NSString, defaultValue (null) is not in the GROUP BY)'
*** First throw call stack:
(
0 CoreFoundation 0x0000000101e35c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001039a0bb7 objc_exception_throw + 45
2 CoreData 0x0000000101a4fbbc -[NSSQLGenerator newSQLStatementForRequest:ignoreInheritance:countOnly:nestingLevel:] + 1724
3 CoreData 0x0000000101a3bdc4 -[NSSQLAdapter _statementForFetchRequest:ignoreInheritance:countOnly:nestingLevel:] + 244
4 CoreData 0x0000000101953e0c -[NSSQLAdapter _newSelectStatementWithFetchRequest:ignoreInheritance:] + 316
5 CoreData 0x0000000101953a86 -[NSSQLCore newRowsForFetchPlan:] + 118
6 CoreData 0x000000010195333c -[NSSQLCore objectsForFetchRequest:inContext:] + 524
7 CoreData 0x0000000101952dbb -[NSSQLCore executeRequest:withContext:error:] + 299
8 CoreData 0x0000000101a2da6c __65-[NSPersistentStoreCoordinator executeRequest:withContext:error:]_block_invoke + 3356
9 CoreData 0x0000000101a36c30 gutsOfBlockToNSPersistentStoreCoordinatorPerform + 192
10 libdispatch.dylib 0x00000001040a2614 _dispatch_client_callout + 8
11 libdispatch.dylib 0x0000000104088002 _dispatch_barrier_sync_f_invoke + 365
12 CoreData 0x0000000101a28245 _perform + 197
13 CoreData 0x0000000101952a58 -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 504
14 CoreData 0x00000001019512ca -[NSManagedObjectContext executeFetchRequest:error:] + 586
15 newapp 0x000000010165ea9a _TFC6newapp25OrderStatusViewController11viewDidLoadfS0_FT_T_ + 1418
16 newapp 0x000000010165efe2 _TToFC6newapp25OrderStatusViewController11viewDidLoadfS0_FT_T_ + 34
17 UIKit 0x00000001028051d0 -[UIViewController loadViewIfRequired] + 738
18 UIKit 0x00000001028053ce -[UIViewController view] + 27
19 UIKit 0x000000010282a257 -[UINavigationController _startCustomTransition:] + 633
20 UIKit 0x000000010283637f -[UINavigationController _startDeferredTransitionIfNeeded:] + 386
21 UIKit 0x0000000102836ece -[UINavigationController __viewWillLayoutSubviews] + 43
22 UIKit 0x00000001029816d5 -[UILayoutContainerView layoutSubviews] + 202
23 UIKit 0x00000001027549eb -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 536
24 QuartzCore 0x0000000106522ed2 -[CALayer layoutSublayers] + 146
25 QuartzCore 0x00000001065176e6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
26 QuartzCore 0x0000000106517556 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
27 QuartzCore 0x000000010648386e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
28 QuartzCore 0x0000000106484a22 _ZN2CA11Transaction6commitEv + 462
29 QuartzCore 0x00000001064850d3 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
30 CoreFoundation 0x0000000101d68ca7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
31 CoreFoundation 0x0000000101d68c00 __CFRunLoopDoObservers + 368
32 CoreFoundation 0x0000000101d5ea33 __CFRunLoopRun + 1123
33 CoreFoundation 0x0000000101d5e366 CFRunLoopRunSpecific + 470
34 GraphicsServices 0x0000000105e09a3e GSEventRunModal + 161
35 UIKit 0x00000001026d48c0 UIApplicationMain + 1282
36 newapp 0x0000000101678747 main + 135
37 libdyld.dylib 0x00000001040d6145 start + 1
38 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
我从这里尝试了解决方案: Swift Core Data - Group doesn't Work
如果需要进一步的信息,请告诉我。
答案 0 :(得分:4)
最后我可以解决这个问题。虽然我没有目标C的知识,但我在目标C中搜索了同样的问题并且想出了解决我在Swift中的问题。它可以帮助可能面临同样问题的人,所以我在这里分享我的解决方案。
问题是propertiesToGroupBy必须包含propertiesToFetch要获取的所有元素。为清楚起见,我在这里分别粘贴这两行代码:
request.propertiesToGroupBy = ["order_num","cust_name"]
request.propertiesToFetch = ["order_num","cust_name"]
观察上面的代码行有两个元素(属性)order_num和cust_name。 (这只是我代码中的一个例子。但我们可以添加任意数量的元素)
以下是我上述问题的完整代码解决方案:
var context : NSManagedObjectContext = appdel.managedObjectContext!
var request = NSFetchRequest(entityName: "TblOrders")
request.returnsObjectsAsFaults = false
request.propertiesToGroupBy = ["order_num","cust_name"]
request.propertiesToFetch = ["order_num","cust_name"]
request.resultType = .DictionaryResultType
context.executeFetchRequest(request, error: nil)!
var res = context.executeFetchRequest(request, error: nil)!
var results = res as NSArray
println(results)