0

Where should I use nil and where should the others Null Nil now i want to assign nil val to a string nsstring=nil;

umairhhhs
  • 400
  • 6
  • 19

3 Answers3

1

For get different of nil and null please refer Objective-C: What's the difference between NULL, nil and @""?

If you want to assign nil or null to NSString you should write

NSString *myStr = @""; // it empty/nil/null

And you can check your string is empty or not by

if([myStr length] == 0) // or if([myStr isEqual:[NSNull class]])
{
  // empty string;
}
else
{
  // not empty string;
}
Community
  • 1
  • 1
iPatel
  • 46,010
  • 16
  • 115
  • 137
0

nil is used to represent a null pointer in objective-c as an object. Nil is to represent a null pointer for a class. NULL is actually the same as nil. It's defined in MacTypes.h as

// MacTypes.h
#define nil NULL

You should also mention NSNull which can be used to represent a null value within NSArray, NSDictionary etc.

jules
  • 840
  • 6
  • 14
  • Well thanks alot.if we have to assign a null value to an object at some index in array then we should use NSNull. – umairhhhs Jan 10 '14 at 12:05
0

All three of these values represent null, or zero pointer, values. The difference is that while NULL represents zero for any pointer, nil is specific to objects (e.g., id) and Nil is specific to class pointers. It should be considered a best practice of sorts to use the right null object in the right circumstance for documentation purposes, even though there is nothing stopping someone from mixing and matching as they go along.

Refer this Difference between nil, Nil, NULL

Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56