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.
- a package name
- the class definition
- definition of instance variables
- definition of an instance method
Thats all we have to do for specifying a java class.
Objective C class
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 – The headerfile specifies the public interface of the class. (developer.apple.com)
- MyClass.m – The implementation file contains the classes implementation. (developer.apple.com)
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.
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.
The super type of a class definition is separated by a colon.
@interface MyClass : NSObject
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 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.
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.
"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 contains the implementation of MyClass.h.
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.
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.