I use the following code to get a NSString from a NSDictionary and then cast it into NSUrl:
NSURL * url = [NSURL URLWithString:[self.item objectForKey:@"website"]];
The NSDictionary self.item comes from a web server and it’s correctly filled using JSON data. All the other objects inside the NSDictionary work perfectly fine.
But sometimes the web server passes a website url with the text “null” because the object has no website filled in. From debugging i learned that the NSURL object can contain a url with the text “null”. But how do i prevent this, or how can i write an if statement that checks this?
I tried the following:
NSString * niks = [eventUrl absoluteString];
if(niks == @"null")
{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Event" message:@"Event heeft geen website" delegate:nil cancelButtonTitle:@"Oke" otherButtonTitles:@"Oke", nil];
[message show];
}
else
{
[webView loadRequest:[NSURLRequest requestWithURL:eventUrl]];
NSLog(@"%@",eventUrl);
}
But this doesn’t work, it always passes the url directly to the webview. Can someone set me in the right direction?
Your comparison;
only compares if the pointers are equal (i.e. if the two are the same string object instance). Since one is a constant and the other is created dynamically from JSON fetched from the server, it’s very unlikely.
To compare the content of two strings, you should instead do;
For the link thirsty, here is the [NSString isEqualToString:] documentation.