02 – Defining Classes

Javaclass


In this part of the “java versus objective c” post-series, i’ll give you a very brief introduction into the difference of defining classes based on java and objective c. This part is not a complete tutorial to learn Objective-C. But it is a short overview how Objective-C classes are defined.

The following image shows a very common java class implementation.

It consists of 4 different parts.

  1. a package name
  2. the class definition
  3. definition of instance variables
  4. definition of an instance method

Thats all we have to do for specifying a java class.

Objective C class

(developer.apple.com)

To define exactly the same class using Objective-C, we have to create 2 different files. A header file and an implementation file.

MyClass.h

If methods or variables should be accessible by other classes, you have to define them in the header file. Otherwise the compiler would send an warning.

The header file is the common interface of this class.

Package

The MyClass.h shown in the image above contains nearly the same information as the java class, except the package. ObjectiveC classes are not seperated in hierarchical package structures like java classes. To avoid duplicate Objective-C classnames, they likely use characters in front of the class name to seperate them. For instance NS.. (NSObject, NSString,…) is used to mark core classes from the Foundation.framework and means NextStep. UI.. for classes from the UIKit.framework.

Class definition

The super type of a class definition is separated by a colon.

@interface MyClass : NSObject

Variables

Instance variables are defined in brackets directly after the class definition.

NSInteger intValue;
NSString *stringValue;

If you’d like you can add a visibility marker to instance variables. (See developer.apple.com) By default, the variables are marked with @protected.

@private, @protected, @public and @package. The semantics of this modifiers is the same as in java.

@protected
NSInteger intValue
@private
NSString *stringValue;

Methods

Methods have to be defined outside the brackets of the class definition.

As in java, methods can be instance methods or static class methods. The first character of the method defines its type.

  • “+” means static class method
  • “-” means instance method
-(void) doSomething:(NSInteger) intValue stringValue:(NSString *) stringValue;

If you are dealing with Objective-C methods for the first time, they will be really confusing. Objective-C tries to be as fluent as possible. So the parameters of the method are not seperated by comma but by words.

The following example should give you an idea how this works.

Javamethod


The method

replaceIntegerByValueInString(int arg1, int arg2, String arg3)

will replace all characters matching arg1 by arg2 in String arg3.

Using java, you are separating the parameter by comma.

Unlikely in Objective-C, you are using a different syntax to define methods, as shown in the following image.

Objective-C method

The parameters are separated by words and not by comma. This allows you a very fluent way of calling methods (sending messages to objects).

"Methodcall in Objective-C":
[self replaceInteger:2 byValue:3 inString:@"Using number 2!"];

"Methodcall in Java":
this.replaceIntegerByValueInString(2, 3, "Using number 2!");

MyClass.m

MyClass.m contains the implementation of MyClass.h.

Variables

Instance variables can not be defined in an implementation class since they are declared in the interface.

For further informations about how to define variables, static variables, properties and constants, i am going to write a separate post in a view days.

Methods

You have to implement all the methods defined in the interface. Otherwise you will get a compiler warning: “Incomplete implementation.”

Additionally you can add local methods which are not declared in the interface file. These methods are only visible inside the current class file.

Advertisements

Tags: , , , , , , , ,

3 Responses to “02 – Defining Classes”

  1. Jorge Campos Says:

    thanks man, great post!
    From Brazil.

  2. karthik g Says:

    i am very very happy that your explanation amazing then i am easy understand then i am more expectation in flos blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: