If you want to get the current date you can just use:
NSDate * now = [NSDate date];
If you want to keep it then retain
it.
NSDate * now = [[NSDate date] retain];
You can't reset NSDate
with init, init is only for initializing the object for the first time.
You could just get another date:
NSDate * now = [[NSDate date] retain];
// use the now object
// need new date
[release now];
now = [[NSDate date] retain];
// once you don't need it release it
[now release];
The date
message returns autoreleased
instance of NSDate
, hence the release
or autorelease
.
The autorelease
is used for cases where you don't want to worry about where exactly you need to release the object - it is put into autorelease pool
. Object in autorelease pool are released after the end of event loop iteration, or when you call release on pool ... (see more in Apple docs about memory management).
Btw. the [NSDate date]
is a convenience method it's probably something like (not quaranteed to be exactly the same but functionally similar to):
- (NSDate *)date
{
return [[[NSDate alloc] init] autorelease];
}