NSData null Check

17,094

Solution 1

When you use the == operator, you are comparing pointer values. This will only work when the objects you are comparing are exactly the same object, at the same memory address.
You can also use isEqualToData: method.this method return YES if the contents of Data are equal to the contents of the receiver, otherwise NO.Two data objects are equal if they hold the same number of bytes, and if the bytes at the same position in the objects are the same.

NSData * dataImage = [[NSData alloc]init];
    if (dataImage == nil) 
    {
        NSLog(@"Image is Assigned .... " );

    }else 
    {
        NSLog(@"Image is Not Assigned ITS NIL.... " );
    }
// Output:Image is Not Assigned ITS NIL....

Solution 2

You have it backwards, you need:

if (dataImage != nil)

Solution 3

try this

if ((dataImage != NULL) && ![v isKindOfClass:[NSNull class]])
{

}

Nothing worked in my case besides this.

Share:
17,094
NSException
Author by

NSException

Newbie to i phone development.

Updated on August 12, 2022

Comments

  • NSException
    NSException over 1 year

    I am assigning image to nsdata object from my Coredata and after assigning it I'm checking whether NSData has image or not.

    If the data is not null, I have to show it. Else I'm recording in NSLog that "No Image is Found".

    App is crashing only when there is no image in the object.

    I tried:

        NSData * dataImage;
        if (dataImage == nil) 
        {
    
    
            NSLog(@"Image is Assigned .... " );
    
        }
        else 
        {
    
            NSLog(@"Image is Not Assigned ITS NIL.... " );
        }
    

    and my device console looks like this :

    Wed Nov 16 12:31:09 unknown ReportCrash[783] <Notice>: Formulating crash     report for process Dial Up App[782]
    Wed Nov 16 12:31:09 unknown com.apple.launchd[1] <Warning>: (UIKitApplication:com.hiteshi.Dial-Up-App[0x1b63]) Job appears to have crashed: Segmentation fault
    Wed Nov 16 12:31:09 unknown SpringBoard[24] <Warning>: Application 'Dial Up App' exited abnormally with signal 11: Segmentation fault
    Wed Nov 16 12:31:10 unknown ReportCrash[783] <Error>: Saved crashreport to /var/mobile/Library/Logs/CrashReporter/Dial Up App_2011-11-16-123107_Priya-Chaturvedis-iPhone.plist using uid: 0 gid: 0, synthetic_euid: 501 egid: 0
    

    I don't know where am I getting wrong. Application is running smoothly on simulator but crashes on device. What am I doing wrong?