1

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){

}
bneely
  • 9,083
  • 4
  • 38
  • 46
user3351727
  • 67
  • 4
  • 14

3 Answers3

1

You can compare URLs using You can convert thumburl into an NSURL using

NSURL thumburl = [NSURL URLWithString:@"http://www.test.com/ur"];

and then compare the two NSURLs using

if([self.bookURL isEqual: thumburl]){
    //They match
}

If they are both strings use

if([self.bookURL isEqualToString:thumbURL]){
    //They match
}

If nothing is displaying in the log the dictionary is probably returning an empty string, check this by logging (or better yet put a break point on the line after your dictionary gets populated)

Ben Avery
  • 1,724
  • 1
  • 20
  • 33
  • 1
    Its also worth mentioning that in general when comparing objects for equality you should not use `==`. `==` is a very shallow equality check. In fact in this case merely checks if both objects occupy the same memory address, nothing more. – cream-corn Mar 06 '14 at 04:16
0

If you want a simple string compare convert with

[someURL absoluteString]

For more complicated cases that arise with URLs check the method provided here... https://stackoverflow.com/a/12332091/1381053

Community
  • 1
  • 1
landon beach
  • 347
  • 4
  • 13
0

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.

coryb
  • 238
  • 3
  • 14