【问题标题】:NSURLRequest Error: "[CoreData length]: unrecognized selector sent to instance"NSURLRequest 错误:“[CoreData 长度]:无法识别的选择器发送到实例”
【发布时间】:2013-01-23 07:12:08
【问题描述】:

我有一个链接,我正在另一个视图中的 QR 码扫描仪中扫描该链接,然后将其保存到 Core Data。我保存它的实体称为“BarCode”,属性为“number”。您将在一分钟内看到的提取结果是来自 QR 码扫描的正确 URL。我只是收到一个错误,就像我在标题中列出的错误一样。它的字面意思是:-[BarCode length]: unrecognized selector sent to instance 0x210645e0

我正在执行这个块,它也会引发错误:

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"BarCode"];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"number" ascending:NO];
request.sortDescriptors = @[sortDescriptor];

NSError *error = nil;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSString *currentURL = [[self.managedObjectContext executeFetchRequest:request error:&error] objectAtIndex:indexPath.row];
if (error) {
    NSLog(@"error fetching data %@ %@", error, error.userInfo);
}

[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:currentURL]]];

我了解从 NSArrayNSString 的转换会导致错误,因为它“无法识别”。但是我做了一些研究,发现错误中的“lenght”属性与NSURLRequests 类中的某些内容有关,但我是该类的新手,所以我不知道我需要什么重写这个块。

我尝试搜索了一下,但找不到涉及NSFetchRequestNSURLRequest 的示例。

我真的希望有人能对这里有所了解。谢谢!

【问题讨论】:

  • BarCode 中的哪个参数代表 url?

标签: ios xcode nsurlrequest nsfetchrequest


【解决方案1】:

您的currentURL 将包含NSManagedObject 子类的实例,而不是NSString。您缺少几行代码:

MyManagedObject *myManagedObject = [[self.managedObjectContext executeFetchRequest:request error:&error] objectAtIndex:indexPath.row];
// This is what you are missing:
NSString *currentURL = myManagedObject.someStringAttribute;
if (error) {
    NSLog(@"error fetching data %@ %@", error, error.userInfo);
}

[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:currentURL]]];

您收到错误 -[BarCode length]: unrecognized selector sent to instance 0x210645e0,因为 [NSURL URLWithString:currentURL] 假定 currentURL 是一个字符串,而 NSURL 尝试进行一些字符串验证。

【讨论】:

  • 很好,但现在我收到-[__NSArrayI number]: unrecognized selector sent to instance 0x1cda7eb0 错误:(
  • 没关系,我忘了添加objectAtIndexPath:。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-21
  • 2015-09-09
相关资源
最近更新 更多