Objective-C, first steps

8th February 2009

Actually I’m wondering how many people start coding Objective-C these days. Especially Flash coders. It must be the App-Store that comes with the iPhone making it really easy to publish your applications to millions of people with a minimum of effort.

Plus Flash 10 doesn’t really come with that much new stuff that you don’t need to study every day to keep track with the development. Compared to the times when AS2 was released for example. It’s not that there is nothing new to learn for me. Especially in the area of Application Frameworks.. I’m still stuck with Cairngorm which does a good job for me though.

I’m just interested in some new things. And Objective-C is something pretty new for me. Its a C-Language and the syntax is fairly different from what you know from ActionScript.

The good thing I realized is: If you understood the core concepts of object oriented programming, you can easily learn Objective-C. It’s more like a different dialect from the same logical language.

Today I wrote my first application. It’s a really simple calculator but it gives you an impression on how the Objective-C syntax looks like.

//
//  main.m
//  Calculator
//
//  Created by Raphael Wichmann on 05.02.09.
//  Copyright Raphael Wichmann 2009. All rights reserved.
//

#import <stdio.h>
#import <objc/Object.h>


@interface Calculator: Object
{
    int _numbera;
    int _numberb;
}

-(void) print;
-(void) setNumberA: (int) a;
-(void) setNumberB: (int) b;

@end


@implementation Calculator

-(void) print
{
    int result = _numbera + _numberb;
    printf("Result: %i + %i = %i", _numbera, _numberb, result);
}

-(void) setNumberA: (int) a
{
    _numbera = a;
}

-(void) setNumberB: (int) b
{
    _numberb = b;
}

@end




int main(int argc, char *argv[])
{
    Calculator *_calculator = [Calculator new];
    [_calculator setNumberA:15];
    [_calculator setNumberB:7];
    [_calculator print];
   
    return 0;
}

With the magic of Objective-C this complex application is capable of adding 15 to 7 which makes 22. Whew ;)

Leave a Message