user1903992
user1903992

Reputation: 465

Pointer initialized

I know in Objective-C and during programming on iOS SDK, pointers are used all the way around.

What is the best way to learn whether a pointer was initialized or not in Objective-C? Check if it is nil?

CSomeClass *p;
//....
if(p==nil)
??

PS: in other words what are the default values in Objective-C for variables? Pointers?


UPDATE

Actually I have the following situation.

Imagine I have some pointers Pointer *p1, Pointer *p2 in some class. Then imagine someone calls this class, i.e., it is a view and must be displayed. Then in my class I want to check that if none had initialised p1 and p2 (e.g., p1 == nil? p2==nil?) I want to display empty text.

Are these some sort of comparisons done in Objective-C? For example, what are the default values of p1 and p2 if they were not initialised? Do values by default get initialized to something in Objective-C? Maybe to null?

Upvotes: 2

Views: 1766

Answers (2)

iDev
iDev

Reputation: 23278

What is the best way to learn whether a pointer was initialized or not in Objective C? Check if it is nil ???

  • Yes, you are correct(By initializing, I am assuming that you meant allocation and not actual initialization of setting default properties). You can check for nil if you have declared it as CSomeClass *p; in ARC. In non-ARC, you should initialize it as CSomeClass *p = nil;.

So here you can do it as,

if (p) { //or if (p != nil)
  //do your operations
} else { //same as if (!p) or if (p == nil)
  //display error message
}

Actually I have following situation. Imagine I have some pointers Pointer *p1, Pointer *p2 in some class. Then imagine someone calls this class, i.e., it is a view and must be displayed. Then in my class I want to check that if none had initialized p1 and p2 (e.g., p1 == nil? p2==nil?) I want to display empty text. Are these sort of comparisons done in ObjC?

  • Yes, that is fine in Objective C. You can check it as if (p1 && p2) or if ((p1 != nil) && (p2 != nil)). Both are fine. In the else part, you can add the empty text which should be displayed.

For example what are the default values of p1 and p2 if they were not initialized? Do values by default get initialized to something in ObjC?? maybe to null?

  • In ARC, it will be nil. In non-ARC you should equate to CSomeClass *p1 = nil; before doing this or else it will be a dangling pointer with some garbage value.

Here is the documentation on ARC.

Upvotes: 3

Stavash
Stavash

Reputation: 14304

Something important to understand here is that Objective-C uses reference counting - this is why the terminology of saying "a pointer is initialized" is a bit problematic.

The way to know if an object even exists (Doesn't mean it's initialized!)

if (!object) {
      NSLog(@"Object is nil");
}

If you wish to release an object, it's always best practice to nil it out. This way, others won't send a message to deallocated instance (causes a nasty crash):

[object release],object = nil;

Upvotes: 1

Related Questions