saintjab
saintjab

Reputation: 1642

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.

Upvotes: 5

Views: 5574

Answers (2)

Lyndsey Scott
Lyndsey Scott

Reputation: 37300

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);
}

Upvotes: 18

Chase
Chase

Reputation: 2304

NSURL contains a method checkResourceIsReachableAndReturnError

NSURL *myURL = [NSURL URLWithString:string];
    NSError *err;
    if ([myURL checkResourceIsReachableAndReturnError:&err] == NO)
    {
        NSLog(@"resource not reachable");
    }

Upvotes: 4

Related Questions