Personally, I had never used a language with a sintax like ObjC (if there’s another one with the same/similar sintax), so, comming from using C and Java, it was quite confusing.
While ObjC uses C as a core, and accessing properties is quite similar as in other languages (object.property;), methods are a little bit different from other languages I’ve used.
Methods
This is the kind of stuff that confused me the most:
1 | [something somethingElse:anotherThing more:Damn]; |
So yea, it’s kinda like WTF?
Basically, what we are doing here is sending a message called “somethingElse”, with parameters “anotherThing” and “Damn”, to an object called “something”.
So let’s take a look at some actual code, this is the way we call an UIAlertView:
1 2 3 | UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"Hello World" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil]; [alert show]; [alert autorelease]; |
Should be more understandable:
First, we are creating a new object called “alert”, of the type “UIAlertView”. Then we are initializing it, then showing it, and then tell it to be autoreleased. (Note, nil is ObjC’s null).
What [UIAlertView alloc] does is tell the OS to make some space in the RAM for a new object of the type UIAlertView.
Next, what initWithTitle:@”Hello”… does is initialize the alert with title “Hello”, message “Hello World”, etc. What [alert show] will do is to show the alert, and [alert autorelease] will autorelease when it’s not needed anymore.
If you go to the frameworks and find UIAlert.h, you’ll find that it has a method like this:
1 | - (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /*<UIAlertViewDelegate>*/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...; |
There you can see where everything came from. In C, it would look like:
1 | id initWithTitle(NSString *title, NSString* message, id *delegate, NSString *cancelButtonTitle, NSString *otherButtonTitles...); |
That’s how methods are in ObjC, basically.
Headers
Now, usually, before using an object, you have to set it up for it to work/look as you want, then you use it, and then you release it, for example, we want to add a label that says Hello World in the middle of the screen:
1 2 3 4 5 6 7 8 9 | //We init the label with the a frame of 320 by 460, set up in the origin UILabel *helloLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)]; //Here we set up some properties, like text, textColor and backgroundColor [helloLabel setText:@"Hello World"]; [helloLabel setTextColor:[UIColor whiteColor]]; [helloLabel setBackgroundColor:[UIColor clearColor]]; //Next we add it to the current view, and then we release it [self.view addSubview:helloLabel]; [helloLabel release]; |
Note that we are doing all the changes before adding it to as a subview, otherwise, the user could see as the properties are changing, and see the object being modified. We don’t want that.
Anyway, if you go to UILabel.h and look for “initWithFrame”, “setText”, “setTextColor”, etc., you won’t find those methods.
The first one is a method from UIView. If you don’t find it in the class you are using, it’s in one of it’s superclasses, when UILabel is declared, it’s declared like this: UILabel : UIView. That means it’s a subclass of UIView, and that it inherited some methods and properties of it (you probably knew this already if you had used OOP before).
The other methods are “setters”. If you go to UILabel.h, you’ll see something like: “@property(nonatomic, retain) NSString *text”. That means that ObjC will automatically create a getter method called “getText”, that will return the value of “text”, and a setter method called “setText”, which will take a NSSting as a parameter and will assign that string to “text”. There are other modifiers (besides nonatomic and retain), but I’ll come by them later.





