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?
Asked
Active
Viewed 1,157 times
1

Jacob Relkin
- 161,348
- 33
- 346
- 320
2 Answers
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
-
Thank you... One more doubt. In string, we can assign an empty character to a string variable. Like NSString *s = @""; For a NSDate variable how can we assign an empty value? – Jan 27 '11 at 06:04
-
@Rajkanth `[NSDate date]` would suffice. – Jacob Relkin Jan 27 '11 at 06:05
-
@Rajkanth But why would you want to do this? – Jacob Relkin Jan 27 '11 at 06:06
-
I have a view to select Date of Birth. Suppose one may want to delete the saved birth date. So only i asked – Jan 27 '11 at 06:29
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