From Java To Objective-C

From Java To Objective C 

My Notes (part 1)


This is a very short introduction from my notes.

A good and complete starting point can be found by google.


I'm a java developer and when I'm going to learn objective-c for the iphone development the matching between the 2 languages became automatic.


Objective C is standard ansi C with object oriented extension.

So many OO arguments found in java can be find in objective C.


In objective C the class usually is split in two file:


  • .h file for the class definition.
  • .m file for the class implementation.


Let's go to see some code, this is only my simple notes.


.h file example


#import <UI/Class.h> //like import in java


//interface is a compiler directive, it tell that there is a class definiton

//myClassName the className

//NSObject the parent object

//<aProtocol> it respect the protocol definition (like abstract class in java)

@interface MyClassName: NSObject <aProtocol>

{

    int count; //a simple primitive type

    id *data; //Weak Type

    NSString *aString; 

//String type. *stay for pointer. all the instance object variable

//use this pointer notation


}


//property declare a class property

//nonatomic or atomic value regard the thread safety in the accessor method

//retain in the set accessor the variable value is retained

@property (nonatomic, retain) NSString aString


//this is a method interface definition

-(id)aMethod:(NSString)*paramName;

@end //end interface declaration

.m file example

#import "MyClassName.h"  //import the class interface definition file

@implementation MyClassName

@synthesize aString; 
//It tells to compiler to make automatically get/set method 
//for property aString

-(id)aMethod:(NSString)*paramName
{
    //insert here the method logic
}

@end


.java Equivalent

import ui.Class;

class MyClassName extends NSObject implements aProtocol
{
    private int count;
    private Object data;
    private String aString;

    public void setAVar(String s) { this.aString = s;}
    public String getAString (return this.aString;)

    Object aMethod (String paramName) { //logic }
}


No comments: