However, when I run this, I get this error:
-[NSURL length]: unrecognized selector sent to instance 0x164dc0
First of all, exception , not error .
When you receive such a message, read what it says:
-[NSURL
The object sent by this message was an NSURL object.
length]:
The message selector was length .
Now, why did you send a length message to an NSURL object? You would not do this, and you would not do it yourself. Something else.
But you are sending a length message to a string object. So, you have an NSURL object, and you passed it somewhere that was expecting a string.
There is only one snippet in the code you showed:
[NSURL URLWithString:first]
An exception indicates that first already an NSURL; this is not a string. You do not need to create an NSURL from it, since it is already one, and trying to treat it as a string in any way will throw an exception.
You can object to my statement based on this previous line:
NSString *first = [urls objectAtIndex:[sender clickedRow]];
Your objection would be that the declaration clearly states that first is a pointer to NSString, so I must be wrong.
But this is not so. You pointed first as a pointer to an NSString. That is, you told the compiler that the first variable will contain a pointer to an NSString.
But then you put a pointer to NSURL in a variable.
In many cases, the compiler warns you that you lied to it, but in this case this did not happen because the object passed through objectAtIndex: which returns id ; thus, the compiler does not know what type of object you are placing in the variable. The compiler, assuming that you have told the truth and really place NSString here, does not warn about this initialization.
But this is not so. This object is NSURL, as you learned at runtime.
Two-fold fix:
- Restore the truth in the declaration by declaring the variable as
NSURL * , not NSString * . - Do not try to create an NSURL here because you already have one.