Asked 7 years ago
7 Jul 2017
Views 1535
jaman

jaman posted

difference between nil and NULL in objective c


    NSInteger  selectedType;




    if( selectedPaymentType != nil ){
}


here crash at warning that comparison between pointer and integer ('NSInteger' and 'void *' )

here nil is not means simply 0
i tried NULL instead nil but same warning
jassy

jassy
answered Apr 24 '23 00:00

In Objective-C, both nil and NULL are used to represent null or empty values. While they have similar functionality, they are not exactly the same.

When you use nil , you are representing a null object pointer that is used specifically for Objective-C objects. On the other hand, NULL is a generic null pointer value used in C and C++, and can be used to represent a null pointer for any type of data, including Objective-C objects.

You can use nil to check whether an object pointer is null, like this:




if (myObject == nil) {
    // myObject is null
}

Similarly, you can use NULL to check whether a pointer is null, like this:



if (myPointer == NULL) {
    // myPointer is null
}


In practice, nil and NULL are often used interchangeably in Objective-C code, although using nil for Objective-C objects is preferred for readability and consistency. The distinction between nil and NULL is largely a matter of convention, and both can be used to represent null or empty values in Objective-C.
Post Answer