Objective-C object allocated

objectobjective c

What's the proper way to tell if an object is allocated in Objective-C?

I've seen in sample code (and this seems to work only if it's never been allocated):

if(!Object) { … }

I've also tried setting Object = nil, but that's a tedious process each time, gets a little bit annoying.

But if I have an object, and I want to allocate and release it more than once, what's the proper way? Thanks!

Best Answer

There is no way to tell whether a variable points to a valid object aside from simply sending it a message and seeing if you crash. Object variables are just pointers. The only way to tell is to use a sentinel value (such as nil). But that shouldn't generally be a problem. If this is giving you trouble, that's evidence of a flaw in your application's design. There's no reason to have variables hanging around that might be initialized or might not.