Sunday, September 25, 2011

I am going to have to learn to read Objective C

Developing with MonoTouch, will require you to at least learn how to read Objective C.
The reason why is that most of the documentation and samples are written in Objective C.
With the latest MonoDevelop+XCode 4.1, interface builder will generate Objective C code, that will be parsed by MonoDevelop and converted into C# for MonoTouch.

Reading Objective C, is therefore handy and required.

If you wrote C or C++ in the past, it is not the end of the world.
If you did not, it is going to feel weird.

Here is a little Objective C sample generated by Interface Builder and its translation in C#.

The h file.
@interface Test4ViewController : UIViewController {
  IBOutlet UITextField *text1;
  IBOutlet UITextField *text2;
}
- (IBAction)copy:(id)sender;
@end
The m file.
An extension m file, contains the code. Generally we use c or cpp extension.
@implementation Test4ViewController
  - (IBAction)copy:(id)sender {
    [text2 setText:[text1 text]];
  }
@end
In C# h files do not exist, in Objective C they still do. The @interface keyword defines
the interface of the class. The properties and signature of the methods are part of the @interface.
The implementation is located in the @implementation block. The sequence :(type)name defines a parameter in a method. Calling a method or an expression needs to be enclosed in []. That is the basic to read

The C# code
class  Test4ViewController : UIViewController {
  UITextField text1;
  UITextField text2;
  IBAction copy(id sender){
    text2.setText(text1.text);
  }
}

No comments:

Post a Comment