1

Suppose if i want to erase a value stored in a NSString variable, i will assign a nil to it. This is possible. But if i want to erase a date variable that already holds some values, what should i do?

Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320

2 Answers2

5

nil is just an object placeholder. You can assign any Objective-C object pointer to it.

So yeah, you can assign a variable of type NSDate * to nil.

If you have ownership of the object however, you should send it the -release message before setting the variable to nil or else the memory pointed to by the variable will be leaked.

Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
3

you can... but it is advisable to release it if it is not autorelease object..

i.e

[dateObject release]; // Only if it is not autorelease object.
dateObject = nil;
Mihir Mehta
  • 13,743
  • 3
  • 64
  • 88
  • If we release a variable, it will remove from the memory. Then how can we assign a nil to it? –  Jan 27 '11 at 06:06
  • @Rajkanth Just because the memory is deallocated doesn't mean that the pointer reference doesn't still exist. – Jacob Relkin Jan 27 '11 at 06:06