iPhone Objective-C syntax equivalence

The syntax with square brackets in Objective-C is one of the reasons most frustration for those coming from other languages ​​(for objects). However, get used to it, you realize how readable the code becomes too with this mix of classical and more distinctly syntax Smalltalk. For example we see how the same procedure can be written in both syntax. Take the initialization of a UIAlertView :

1
2
3
4
5
6
7
8
[ [ UIAlertView alloc ] initWithTitle : @ "Titolo" UIAlertView MyAlert * = [[UIAlertView alloc] initWithTitle: @ "Title"
"Messaggio" message: @ "Message"
delegate: self
"Annulla" cancelButtonTitle: @ "Cancel"
"Ok" , nil ] ; otherButtonTitles: @ "OK", nil];
; myAlert.tag = 1;
; [MyAlert show];
; [MyAlert release];

The setting of the tag potavamo it even so:

1
2
3
4
5
6
7
8
[ [ UIAlertView alloc ] initWithTitle : @ "Titolo" UIAlertView MyAlert * = [[UIAlertView alloc] initWithTitle: @ "Title"
"Messaggio" message: @ "Message"
delegate: self
"Annulla" cancelButtonTitle: @ "Cancel"
"Ok" , nil ] ; otherButtonTitles: @ "OK", nil];
1 ] ; [MyAlert SETTING: 1];
; [MyAlert show];
; [MyAlert release];

Same thing in reading during the event:

1
2
3
4
5
void ) alertView : ( UIAlertView * ) alertView clickedButtonAtIndex : ( NSInteger ) buttonIndex { - (Void) alertView: (UIAlertView *) alertView clickedButtonAtIndex: (NSInteger) {buttonIndex
alertView.tag == 1 ) { // ... if (alertView.tag == 1) {/ / ...
/ / Or
alertView tag ] == 1 ) { // ... if ([alertView tag] == 1) {/ / ...
}

Note: Not all properties or methods have this dual syntax. In the case of ' UIAlertView we have this dual functionality. For other objects, or in other cases, however, only one of the two might work ... but do not ask why (depending on how the developers have defined the interface).

There are no comments for this post

Leave a comment

XHTML TAG PERMIT: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL