Objective-C: Declaring and Using Classes

John C. Bland II

Learning Objective-C for me is about learning how to read the code in comparison to other languages I know. Here is what I know so far about classes.

I may say this in all of my posts but this is just weird to me. I won’t get long winded here but I do want to post a few examples. Keep in mind I’m posting these as I learn so there is a lot of knowledge to be had. With that said, you might see me do a part 2 of this post clarifying something I may have posted here.

Classes
I believe there is another way but for now I’m going to state how it is, at this point, in the book I’m reading.

//imports
@interface Name: ParentClass
{
//properties
type variable
type variable
type variable
...
}
 
//methods
(scope) (return type) methodName;
(scope) (return type) methodName;
...
@end
 
@implementation ClassName
(scope) (return type) methodName
{
//do something
//return variable;
}
 
(scope) (return type) methodName: (var type) varName
{
//do something
//return variable
}
@end

This is the core structure but what I expect is to not have to define an interface in the class file. This way we can create interfaces separate and implement in multiple classes. I assume you can but I haven’t gotten that far in the book. (update: found a link here that interfaces as separate files, as expected)

@interface – References the interface section
@implementation – References the interfaces implementation (the class itself)

Scoping
I’m praying there is some form of public, private, protected, etc but so far there are two scopes: – and +. HUH? I know…let me explain. ;-) (update: found a link here that shows the scopes I “prayed” for above)

- denotes instance level methods/variables (in ECMA, someClassInstance.methodName)
+ denotes class level methods/variables (in ECMA, SomeClass.methodName)
Instantiating and Using

//allocates memory for a MyClass instance and returns a new instance of MyClass
myClassInstance = [MyClass alloc]; //alloc is a class level method; called on the class only
//initializes the class; assuming this is a constructor of sorts
myClassInstance = [myClassInstance init]; //init is an instance method; called on instances only

Note: Extending NSObject gives you the core logic for allocating memory so you don’t have to define it or the init.

That isn’t bad at all. I can read it and understand. What gets me is when you start compounding method calls. The above can also be written like this:

myClassInstance = [[MyClass alloc] init];

The order is [inner] to [[..] outer] so allocate first then call init. In ECMA terms:

myClassInstance = new MyClass();

:-) I started to write .init() on the end but .init() is the same as the constructor, from what I see. :-)
The next thing is to call an instance method from a class.

//get instance of class
myClassInstance = [[MyClass alloc] init];
 
//call method on class; void return type
[myClassInstance doSomething];
 
//call method on class; returns int
(int) myResult = [myClassInstance getMyInt];

In ECMA:
myClassInstance.doSomething();

int myResult = myClassInstance.getMyInt();//java/c#
var myResult:int = myClassInstance.getMyInt();//as3

I’m going to stop here for classes. There is a lot more to go over but this helped me understand some of the code I see here/there and I wanted to blog it so I had a running log of my learning points. More to come.

Update

I tweaked the post to use the language syntax highlighter. It makes it easier to read the code.




This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

One Response to “Objective-C: Declaring and Using Classes”

Leave a Reply

  • Advertisements

  • RSS John And Season

  • Get Adobe Flash playerPlugin by wpburn.com wordpress themes