- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试以最基本的形式使用核心数据分组功能:
- (NSFetchedResultsController *)fetchedResultsController {
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSExpression *reviewNumKeyExpression = [NSExpression expressionForKeyPath:@"review_num"];
NSExpression *reviewMaxExpression = [NSExpression expressionForFunction:@"max:" arguments:[NSArray arrayWithObject:reviewNumKeyExpression]];
NSExpressionDescription *maxReviewED = [[NSExpressionDescription alloc] init];
[maxReviewED setName:@"maxReview"];
[maxReviewED setExpression:reviewMaxExpression];
[maxReviewED setExpressionResultType:NSInteger32AttributeType];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Group" inManagedObjectContext:managedObjectContext];
[fetchRequest setResultType:NSDictionaryResultType];
[fetchRequest setReturnsDistinctResults:YES];
[fetchRequest setEntity:entity];
NSAttributeDescription *groupId = [entity.propertiesByName objectForKey:@"group_id"];
NSArray *propertiesToFetch = [NSArray arrayWithObjects:groupId, maxReviewED, nil];
[fetchRequest setPropertiesToFetch:propertiesToFetch];
[fetchRequest setPropertiesToGroupBy:propertiesToFetch];
...
运行此代码时,出现以下错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid keypath expression ((), name maxReview, isOptional 1, isTransient 0, entity (null), renamingIdentifier maxDepartment, validation predicates ( ), warnings ( ), versionHashModifier (null) userInfo { }) passed to setPropertiesToFetch
我想我在 API 中遗漏了一些东西,所以非常感谢任何帮助!
最佳答案
我认为错误消息是错误的:问题出在 setPropertiesToGroupBy
,而不是 setPropertiesToFetch
。您不能按计算值 (maxReview
) 分组,但我怀疑您是否真的想这样做:如果您想要每个 groupId
的 review_num
的最大值>,你只需要propertiesToGroupBy
中的groupId
:
NSAttributeDescription *groupId = [entity.propertiesByName objectForKey:@"group_id"];
NSArray *propertiesToFetch = [NSArray arrayWithObjects:groupId, maxReviewED, nil];
NSArray *propertiesToGroupBy = [NSArray arrayWithObjects:groupId, nil];
[fetchRequest setPropertiesToFetch:propertiesToFetch];
[fetchRequest setPropertiesToGroupBy:propertiesToGroupBy];
关于ios - setPropertiesToFetch 返回 'Invalid keypath expression' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35263730/
Foo 是我的 MongoDB 集合。我只有一个文档: { 0: {code: "basic", caption: "basic", points: 100}, 1: {code: "gold
let arr = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)] arr.map(\.0) // [1, 2, 3, 4, 5] 效果很好。但是下面的代码无法编译:
我的 swift 应用程序在运行时失败。我该如何解决这个问题? 我已经检查过,avgTime_text 定义明确。 我不知道如何读取 xcode 中的堆栈跟踪,有人可以提供一些指导吗? 2016-02
我有一些 JSON 看起来像这样,它被声明为一个名为“jsonObjSuper”的变量: { "Watchlist": "My Watchlist", "Instruments": { "instru
我有一个应用程序,我在其中创建一个带有密码的 HD 钱包,并将公共(public)主 key 保存到用户的数据库中。 然后,我希望用户能够将钱包备份到自己的钱包中,在本例中我使用的是Wallet32。
看看下面的代码: struct Something { var s: String! // Implicitly Unwrapped Optional } func bind(keyPath: W
我有一棵包含多个项目和多个级别的完整树,该树是使用 ajax 调用构建的并使用惰性节点 所以现在我想添加这个函数,这样我的树就可以加载和选择项目,如果我给出完整的路径,但在我可以选择项目之前,我需要确
如何从嵌套类创建 KeyPath?我不知道如何使用反斜杠访问嵌套类。 示例代码: class Config { var clients: Array = [Client()] // N
一个快速的问题。 当您创建一个 IndexedDB 并指定一个特定的 keyPath 时,你还需要用同一个key来创建和索引吗? 我认为 keyPath本身就是一个索引,但我不知道也没有找到任何关于它
这两种格式之间的有用区别是什么: request.sortDescriptors = [NSSortDescriptor(key:"dateCreated", ascending: false)] 和
我正在尝试为获取的结果 Controller 创建一个 NSPredicate,该 Controller 应该只观察具有关系(不为零)并且关系计数大于 0 的对象。 我已经了解了如何使用字符串作为谓词
在 Swift 4 中,Foundation 团队的许多人都讨论过与 Swift 3 相比,使用 keyPaths 要容易得多。这引出了一个问题……什么是 keyPath?说真的,我找不到任何明确的资
是否可以使用新的 #keyPath 关键字对 Swift 3 中的 CATransform3D 属性使用增强的 key 路径(如 here 所述)? 换句话说,要替换 let scaleAnimati
WiX 的“KeyPath”属性是什么?特别是,它如何应用于以下方面: 最佳答案 如explained by Rob Mensching : The KeyPath for a Com
有没有办法在 Swift 4 中从字符串创建 Keypath,以通过路径或变量名访问结构中的值 Finally I found out that I should use CodingKeys ins
当您在 IndexedDB 中创建一个 objectStore 或索引时,您指定了一个“keyPath”,它说明了在您的对象中的何处查找应该是存储或索引上的键的值。我有看起来像这样的对象: {doc:
我正在尝试通过使用 KeyPaths 来更新存储在类的数组属性中的值。这是我的代码: func listenAndUpdateDocuments( _ property: ReferenceW
我有 2 种类型,A 和 B,它们实现相同的方法并具有相同的属性。我已经定义了一个扩展来获取每个 A 和 B 的子属性中的值。我想知道是否有办法将这 2 个扩展减少到 1 个方法。想象一下,有更多类型
当我试图在对象 ID 之后获取一些对象时,我遇到了这个奇怪的问题。该错误提示没有 objectID 键路径这样的东西,但托管对象应该对此做出响应。该错误并非始终引发,这让我认为这可能是并发问题,尽管我
尝试用这个编辑 AKCompressor 属性: let compressor = AKCompressor() compressor[KeyPath: \AKCompressor.threshold
我是一名优秀的程序员,十分优秀!