Search code examples
iosobjective-cnsurlnsurlrequest

NSURL equal to server URL


I'm getting url from my server. I used this code

self.bookURL = [aDictionary objectForKey:@"bookurl"];

The url is http://www.test.com/url. If this URL matches, I need to display a UIButton. How can I match this URL with self.bookURL? I used below NSLog, nothing displays in the log.

NSLog(@"book url is %@",bookURL);
NSString *thumburl=@"http://www.test.com/ur";
if(bookURL==thumburl){

}

Solution

  • You cannot compare strings (or objects for that matter) with ==.

    You can either convert the NSString to an NSURL and compare like this:

    NSURL *thumburl = [NSURL URLFromString:@"http://www.test.com/ur"];
    
    if ([self.bookURL isEqual:thumburl]){
         //do something
    }
    

    This assumes that the value bookurl stored in the NSDictionary is an object of type NSURL. If you want to do a string comparison between two NSStrings (instead of working with NSURL), then you can do the following:

    if ([self.bookURL isEqualToString:thumburl]){
         //do something
    }
    

    This second example assumes that the self.bookURL object that you are getting back is NSString instead of NSURL.