Cocoa Webkit错误? [英] Cocoa Webkit bug?

查看:120
本文介绍了Cocoa Webkit错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码从数组中获取URL并将其存储为字符串,然后使用该字符串来加载URL。

  NSString * first = [urls objectAtIndex:[sender clickedRow]]; 
NSLog(@%@,first);
[[webview mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:first]]];但是,当我运行这个我得到这个错误:


$ b

$ b

   -  [NSURL length]:无法识别的选择器发送到实例0x164dc0 

本例中的链接为 http://www.digg.com



我知道错误在行

  NSString * first = [urls objectAtIndex:[sender clickedRow]]; 

因为我试图将字符串的值直接设置为URL而不是数组,但是,当我运行这个,我得到这个错误:


$


b $ b

   -  [NSURL length]:无法识别的选择器发送到实例0x164dc0 


首先,这是一个异常,而不是错误< a>。



当您收到类似这样的讯息时,请阅读说明:

 code>  -  [NSURL 

发送此消息的对象是NSURL对象。 / p>

  length]:

邮件的选择器是 length



现在,为什么要发送 length 消息到NSURL对象?你不会,你自己没有这样做。



但您会向字符串对象发送 length 。所以,你有一个NSURL对象,并且你传递它预期一个字符串的地方。



你显示的代码只有一个段落:

  [NSURL URLWithString:first] 

异常告诉你第一已经是NSURL;它不是一个字符串。您不需要从中创建NSURL,因为它已经是一个,并试图以任何方式将其视为字符串将导致异常。



您可以


  NSString * first = [urls objectAtIndex:[sender clickedRow]]; 


您的反对意见是声明明确表示第一是指向NSString的指针,所以我一定错了。



但是不是这样。 首先声明为指向NSString的指针。也就是说,你告诉编译器变量第一将保存一个指向NSString的指针。



但是



在许多情况下,编译器会警告你,你已经骗了它,但它没有在这个case因为对象通过 objectAtIndex:,它返回 id ;因此,编译器不知道要将哪个类型的对象放入变量。编译器,假设你说实话,并且真的在这里放了一个NSString,不会警告这个初始化。



但你不是。



修复是双重的:


  1. 通过声明变量为 NSURL * ,而不是 NSString *

  2. 请勿尝试在此处创建NSURL,因为您已有一个NSURL。


I have some code that gets the URL from an array and stores it as a string then uses that string to load up the URL.

NSString *first = [urls objectAtIndex:[sender clickedRow]];
NSLog(@"%@", first);
[[webview mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:first]]];

However, when I run this I get this error:

-[NSURL length]: unrecognized selector sent to instance 0x164dc0

The link in this case is http://www.digg.com

I know that the bug is in the line

NSString *first = [urls objectAtIndex:[sender clickedRow]];

because I tried setting the string's value directly to the URL instead of the array and it worked.

解决方案

However, when I run this I get this error:

-[NSURL length]: unrecognized selector sent to instance 0x164dc0

First off, that's an exception, not an error.

When you get a message like this, read what it says:

-[NSURL

The object you sent this message to was an NSURL object.

length]:

The selector of the message was length.

Now, why would you send a length message to an NSURL object? You wouldn't, and you didn't do so yourself. Something else did.

But you would send a length message to a string object. So, you have an NSURL object, and you passed it somewhere that expected a string.

There's only one passage in the code you showed:

[NSURL URLWithString:first]

The exception tells you that first is already an NSURL; it is not a string. You do not need to create an NSURL from it, since it already is one, and trying to treat it as a string in any way will cause an exception.

You may be about to object to my claim on the grounds of this previous line:

NSString *first = [urls objectAtIndex:[sender clickedRow]];

Your objection would be that the declaration clearly says that first is a pointer to an NSString, so I must be wrong.

But that is not so. You declared first as a pointer to an NSString. That is to say, you told the compiler that the variable first would hold a pointer to an NSString.

But then you put a pointer to an NSURL into the variable.

In many cases, the compiler would warn you that you have lied to it, but it didn't in this case because the object came through objectAtIndex:, which returns id; thus, the compiler doesn't know what type of object you're putting into the variable. The compiler, assuming that you told the truth and really are putting an NSString here, does not warn for this initialization.

But you're not. The object is an NSURL, as you found out at run time.

The fix is two-fold:

  1. Restore truth to the declaration, by declaring the variable as NSURL *, not NSString *.
  2. Don't attempt to create an NSURL here, because you already have one.

这篇关于Cocoa Webkit错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆