Search code examples
iosif-statementnsdata

Conditional statement to check if NSData contains certain value


I tried the code below and it is not working. I am trying to test whether NSData is nil or not to assign to image. This is the code i have tried:

NSData * imageData = [NSData dataWithContentsOfURL:imageURL];

NSLog(@"image data %@",imageData);

if ( imageData== nil) {
    NSLog(@"NO IMAGE");
    imageSection.image = [UIImage imageNamed:@"noPic"];
}else{
imageSection.image = [UIImage imageWithData:imageData];
}

The logged image data returns the response below when an image is not present:

imageData <>

So i was wondering how to check if if NSData contains this.


Solution

  • use this:

    if (imageData.length > 0) {
        //imageData have some value
    }