views:

81

answers:

4

Im new to obj-c and need some help with this code

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];  
[dateFormatter setDateFormat:@"E, d LLL yyyy HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:currentDate];

the variable date cannot be nil. how can I make it so that date = current time when it's unable to format the string? can i use try/catch? how?

A: 

Could try this :

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];   
[dateFormatter setDateFormat:@"E, d LLL yyyy HH:mm:ss Z"]; 
NSDate *date = nil;
@try
{
  date = [dateFormatter dateFromString:currentDate]; 
}
@catch (NSException *exception)
{
  date = [NSDate date];
}
OMG_peanuts
That would be bad because the object returned in the `@try` block is autoreleased and the object returned by the `@catch` block is not. You'd be better off with `[NSDate date]` in the `@catch` block.
Paul
Thank you for that. Edited.
OMG_peanuts
@paul how is [NSDate date] different in terms of memory management? im new.
Yaso
@Yaso When you create an object using the alloc/init form: `[[NSDate alloc] init]` the memory is allocated on the heap (think of the C `malloc` or C++/Java/C# `new` operator) and you are responsible for deallocating it later.Apple's convention is that class-level object creation, `[NSDate date]` in this case, is added to the autorelease pool. This would be similar to creating an object on the stack in C++. As with stack objects, autoreleased objects are deallocated as soon as the variable pointing to the object goes out of scope.
Paul
@Paul : "Autoreleased objects are deallocated as soon as the variable pointing to the object goes out of scope."Err, I thought that autoreleased objects were deallocated when the last autorelease pool of the call stack is drained. Is that wrong ?
OMG_peanuts
A: 

This should doesn't work:

NSDate *date = [dateFormatter dateFromString:currentDate] || [NSDate date];
Douwe Maan
Why the downvote?
Douwe Maan
The result of [dateFormatter dateFromString:currentDate] || [NSDate date] is BOOL isn't it?
Vladimir
No it's not afaik, it will be either `[dateFormatter dateFromString:currentDate]`, or if that results boolean false (`nil`, in this case) it will be `[NSDate date]`. I just tested it in JavaScript, and it works there, but I can't say with 100% certainty it works the same in (Objective-)C...
Douwe Maan
@Douwe: It's always `int` in (Objective-)C. Don't assume Javascript == C.
KennyTM
no, its not the same :) you must need ternary operator to do what you want (see my answer)
Vladimir
@KennyTM Okay, won't do it again :) It's just that I came across this in not only JS, and I thought it would work in most if not all programming languages. Looks like I was wrong.
Douwe Maan
+4  A: 

Why just not check the date returned from formatter and if it is nil assign current date to it?

NSDate *date = [dateFormatter dateFromString:currentDate];
if (!date)
   date = [NSDate date];

Or 1-liner using ternary operator (and its gcc extension):

NSDate *date = [dateFormatter dateFromString:currentDate]?:[NSDate date];
Vladimir
I didn't know about that GCC extension. Very, very cool. Other languages have something similar, like C# with its `??` operator.
Paul
how would you write this in c#?
Yaso
A: 

you should not do that NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; because the autorelease makes the object lifetime uncertain. Omit the autorelease and release the object instead when you are done with it. (In case you are unsure: you see allocation like that (with autorelease) quite frequently when you give the object to a property which has retain enabled)

Russo