NSSortDescriptor 错误:“[NSSortDescriptor 计数]:无法识别的选择器发送到实例”

Posted

技术标签:

【中文标题】NSSortDescriptor 错误:“[NSSortDescriptor 计数]:无法识别的选择器发送到实例”【英文标题】:NSSortDescriptor error : " [NSSortDescriptor count]: unrecognized selector sent to instance " 【发布时间】:2013-05-09 23:52:01 【问题描述】:

我正在尝试使用 fetch 请求设置 NSFetchedResultsController,但我不断收到错误消息:

"[NSSortDescriptor count]: 无法识别的选择器发送到实例"

下面是我的代码。我的模型只有一个我要获取的记录。我也在关注 Paul Heagarty 的 2010-2011 Core Data 演示视频来设置我的核心数据。

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Temple"];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
request.predicate = nil;
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.eventDatabase.managedObjectContext sectionNameKeyPath:nil cacheName:nil];

【问题讨论】:

您确定将sortDescriptors 传递给您的请求,而不是sortDescriptor(没有s)吗?听起来如果您提供一个需要 NSArray 的单个 NSSortDescriptor 实例会发生什么。 是的,检查了很多次,更改了代码并重写了它。难道是因为数据库中只有一条记录 谢谢蒂姆..我解决了..你让我再次查看代码,我发现另一段代码与你提到的完全一样。谢谢 可能应该写下你自己的答案并接受它或删除它......如果你认为它在未来不会帮助任何人。 【参考方案1】:
// CAUSE OF ERROR: Assigning NSSortDescriptor to a property that expects an NSArray
fetchRequest.sortDescriptors = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];

// SOLUTION: Wrap your sort descriptor in an array, fixes error
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]];

【讨论】:

以上是关于NSSortDescriptor 错误:“[NSSortDescriptor 计数]:无法识别的选择器发送到实例”的主要内容,如果未能解决你的问题,请参考以下文章

AWS GovCloud 上 docker alpine wildfly 中的 NSS/PKCS11 错误

cURL SSL连接错误35与NSS错误-5961

不支持的 NSSortDescriptor(不支持比较器块)

PHP CURL“NSS:找不到客户端证书(未指定昵称)”问题

CKQuery 的 distanceToLocation 上没有 NSSortDescriptor?

使用 OpenSSL 生成的 SSL 证书不适用于 NSS