I would say this is pretty much a style / readability thing, although I do see nearly all objective-c/cocoa formatted as per "METHOD_002". I am just curious if "METHOD_001" would be considered bad style, there are advantages to having all the declarations at the top of a method, but then again there are disadvantages in terms of readability if your not declaring objects where using them?
METHOD_001
-(IBAction)dateButtonPressed {
NSDate *dateSelected;
NSString *dateString;
NSArray *dateItems;
NSString *alertMessage;
UIAlertView *alert;
dateSelected = [datePicker date];
dateString = [[NSString alloc] initWithFormat:@"%@", dateSelected];
dateItems = [dateString componentsSeparatedByString:@" "];
alertMessage = [[NSString alloc] initWithFormat:@"Date: %@ Time: %@", [dateItems objectAtIndex:0], [dateItems objectAtIndex:1]];
alert = [[UIAlertView alloc] initWithTitle:@"You Selected"
message:alertMessage
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
[alertMessage release];
[dateString release];
}
METHOD_002
-(IBAction)dateButtonPressed {
NSDate *dateSelected = [datePicker date];
NSString *dateString = [[NSString alloc] initWithFormat:@"%@", dateSelected];
NSArray *dateItems = [dateString componentsSeparatedByString:@" "];
NSString *alertMessage = [[NSString alloc] initWithFormat:@"Date: %@ Time: %@", [dateItems objectAtIndex:0], [dateItems objectAtIndex:1]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You Selected"
message:alertMessage
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
[alertMessage release];
[dateString release];
}
gary