Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing empty NSURL

I need your help. I have an NSURL object I get from this

NSURL *myImageUrls = [[feeds objectAtIndex:indexPath.row] objectForKey: @"url"];
if(myImageUrls == nil){
    NSLog(@"URL empty %@", myImageUrls);
}
else{
    //NSURL *url = imageUrls;
    NSLog(@"Image URL:%@", myImageUrls);
}

The NSLog shows Image URL: meaning the myImageUrls is empty but the if statement never get printed. How do I get it to print if the NSURL object is empty. I have tried myImageUrls == null and [myImageUrls length] =0 but still the if block never get printed.

like image 537
saintjab Avatar asked Nov 29 '22 10:11

saintjab


1 Answers

If your NSLog prints "Image URL:" that doesn't indicate that your NSURL is nil or null; it indicates that your NSURL's string is empty. If your NSURL was null but still executing that second conditional, your NSLog would print: "Image URL: (null)" instead.

To check whether your NSURL's string is empty (since it seems as if you're storing NSURL's in all indexes of your array, but simply storing an NSURL with an empty string at the "empty" indices), change your conditional to the following:

if(myImageUrls.absoluteString.length == 0){
    NSLog(@"URL empty %@", myImageUrls);
}
else{
    //NSURL *url = imageUrls;
    NSLog(@"Image URL:%@", myImageUrls);
}
like image 96
Lyndsey Scott Avatar answered Dec 05 '22 13:12

Lyndsey Scott