DevC
DevC

Reputation: 7022

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.

Upvotes: 1

Views: 1809

Answers (2)

Helge Becker
Helge Becker

Reputation: 3243

An NSData object can have no data, so comparing the reference to nil wont work. But NSData has a length property. If that is greater than 0 then there is data in the object.

Upvotes: 2

Bhumeshwer katre
Bhumeshwer katre

Reputation: 4671

use this:

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

Upvotes: 7

Related Questions