Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nashyo

macrumors 6502
Original poster
Oct 1, 2010
299
0
Bristol
What's wrong with this code

Code:
NSLog(@"The link is %@", [selectedContent objectForKey:@"link"]);
        
    NSString *string = [[NSString alloc] initWithString:[selectedContent objectForKey:@"link"]];
    NSLog(@"The string is %@", string);
    NSURL *myURL = [[NSURL alloc] initWithString:string];
    NSLog(@"The NSURL is %@", myURL);
    NSURLRequest *myRequest = [[NSURLRequest alloc] initWithURL:myURL];

Console:
The link is http://www.apple.com
The string is http://www.apple.com
The NSURL is (null)
 

chown33

Moderator
Staff member
Aug 9, 2009
10,740
8,416
A sea of green
Make sure the string doesn't have any unexplained characters. Use one of the percentEncoding methods of NSString to produce a percent-encoded form, then NSLog that string.
 

nashyo

macrumors 6502
Original poster
Oct 1, 2010
299
0
Bristol
Thanks for the feedback chown33. However, I have literally copy and pasted the string, as it appears in the dictionary and the web view loads no problem at all. I'm baffled as to why the above object is null when I call the dictionary directly.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,740
8,416
A sea of green
Dig deeper.

Set aside your expectations, and only gather individual facts which are supported by evidence. Attempt to prove or disprove this assertion:
"The object whose key is 'link' is an NSString, and it contains only valid URL-safe characters."​
It's clear that you believe this to be true, but belief isn't evidence.

Use the debugger. Make sure that what you think is an NSString is really an NSString. This line is not guaranteed to produce an actual NSString object in the variable 'string':
Code:
    NSString *string = [[NSString alloc] initWithString:[selectedContent objectForKey:@"link"]];
Nor does NSLog'ing require an NSString. Any NSObject, regardless of type, has a -description method that returns NSString. That's what %@ uses, and that method IS guaranteed to produce an NSString.

Do like I said and make sure there are no unexpected characters. After ensuring you have a real NSString, check its length, then break it apart into characters, if necessary, and confirm that each is a valid URL character.
 
Last edited:

nashyo

macrumors 6502
Original poster
Oct 1, 2010
299
0
Bristol
Dig deeper.

Set aside your expectations, and only gather individual facts which are supported by evidence. Attempt to prove or disprove this assertion:
"The object whose key is 'link' is an NSString, and it contains only valid URL-safe characters."​
It's clear that you believe this to be true, but belief isn't evidence.

Use the debugger. Make sure that what you think is an NSString is really an NSString. This line is not guaranteed to produce an actual NSString object in the variable 'string':
Code:
    NSString *string = [[NSString alloc] initWithString:[selectedContent objectForKey:@"link"]];
Nor does NSLog'ing require an NSString. Any NSObject, regardless of type, has a -description method that returns NSString. That's what %@ uses, and that method IS guaranteed to produce an NSString.

Do like I said and make sure there are no unexpected characters. After ensuring you have a real NSString, check its length, then break it apart into characters, if necessary, and confirm that each is a valid URL character.

I haven't had time to put this into practice but I intend to check the string for unsafe URL characters in a few hours. However, I do have some evidence that I can't explian...

This returns null
NSURL *myURL = [NSURL urlwithstring:[selectedContent objectforkey:mad:"link"];
If I extract the NSLog string in the console by physically highlighting and copying the NSLog message for

Code:
NSLog("%@", [selectedContent objectforkey:@"link"]);

Then paste that as the string for the following

NSURL *myURL = [NSURL urlwithstring:mad:"pasted NSLog string"];

the NSURL is not null. Why is there a difference?
 

chown33

Moderator
Staff member
Aug 9, 2009
10,740
8,416
A sea of green
I haven't had time to put this into practice but I intend to check the string for unsafe URL characters in a few hours.

That's backwards. The first thing you need to do is confirm that the object you assume is a string is really an NSString. If it's not, then you can't possibly "check the string" for unsafe URL characters, because it's not a string.

Here's the central point: you still believe the object you're working with is an NSString. Based on what evidence? You haven't described where the dictionary containing the "link" key came from, how it was built, nor how the object associated with "link" was obtained. Maybe the source of that dictionary isn't checking the kinds of objects it's putting in the dictionary. Or maybe you've just blindly assumed that some object whose %@ looks like a string is always going to be an NSString.

You keep treating the object like it's a string because you still believe it's a string. Forget that assumption. How do you identify what the actual class of an object really is? Maybe use the debugger. Or try logging the object's -class instead of the object itself:
Code:
NSLog( @"mystery class: %@", [[selectedContent objectforkey:@"link"] class] );
This is what I mean about gathering evidence. The output is evidence that the object in question is or isn't an NSString. If it's not an NSString, then you can proceed accordingly.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.