Thursday, March 24, 2011

Interface

Objective-C, as you might have guessed, is an object oriented programming language.
Those of you who haven't had experience working with objects, here's a summary:
An object is basically a package of information with associated operations.

Let's say that a person is an object. A person has an age, weight, height. A person eats and sleeps.
We have three variables: age, weight, height
We have two functions: eat, sleep
When a person performs an action, the attributes of that person may change. For instance, if a person eats, their weight will probably change.

Examine the following code:

1 @interface Person: NSObject{
2 int age;
3 int weight;
4 int height;
5 }
6 -(void) setAge: (int) a;
7 -(void) setWeight: (int) w;
8 -(void) eat;
9 @end



Line 1 has @interface, which means you're creating a new type of object. The name of the object type is "Person". NSObject what most Objective-C objects inherit from (complicated stuff, I won't go over it now).

Lines 2-4 are the declarations for the variables we discussed earlier. These are all integer (number) data types.

Lines 6-8 are function declarations. The (void) before each of them indicates that they do not have a return type (we'll cover this later). Set age and set weight both take an argument (int) a and (int) w. To set the age and weight of the person, you need to give the object a number. The integer you pass in will eventually become the age or weight (respectively) of the person.


Line 9 has @end which means that the interface block has ended.

The interface is similar to a header file in C and C++. Prototypes are set for functions and variables are declared, but no operations are coded yet. Stay tuned and we'll write the code for these prototypes.

11 comments:

  1. I took one look at that and said no, but kept reading and...wow, you explain this stuff pretty well. I'd really love to break into programming someday, any recommendations as to where I should start?

    ReplyDelete
  2. seems pretty simple and streamlined. only thing i've ever coded in is c++

    ReplyDelete
  3. Objective C is what is used when coding iPhone-apps, am I right? If so, would you be kind enough to share a link or two as of where to get started on such things?
    :D

    ReplyDelete
  4. @Justin
    If you read my entry on Xcode, that's the programming environment I use and the starting point for this series. It requires Mac OS X.

    @Sean
    "High level" programming languages are very easy to use. HTML is an example.
    Java is a medium level language. They hide a lot of confusing things from the programmer (such as memory addresses, etc).
    C and C++ are extremely low level and difficult to learn. I started with these, but because of that, everything else is much friendlier and easier.

    ReplyDelete
  5. Gonna look into this when I get the time - I'm more of a Java / PHP guy, but I've wanted to learn C/Objective C/C++ .. one of the three is a good step up. :)

    ReplyDelete
  6. Very interesting. I've wanted to learn obj-c for a while. I'll keep checking back often!

    ReplyDelete
  7. i wish i could program something in c, was always too lazy to learn it

    ReplyDelete
  8. Wow, you explained that really well.

    ReplyDelete
  9. I really need to get into programming, even if it is just scouring your posts which I barely understand.

    ReplyDelete
  10. I have never seen objective-c code before.

    ReplyDelete