I would say that it is pretty much style / readability, although I can see almost all objective-c / cocoa in the "METHOD_002" format. I'm just wondering if "METHOD_001" would be considered a bad style, there are advantages to having all the declarations at the top of the method, but again there are disadvantages in terms of readability if you do not declare objects where you use 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
source
share