Objective-C Program structure

Introduction

In this post I am going to cover some of the basic syntax of Objective-C contrasting it with Java. I will cover the different program structures and how this effects variable declaration.

Program Structure:

Objective-C is structured much like the C language it is based off of. Both languages use header files (.h)and ‘main’ files that hold implementation (.m files). Java by contrast only uses one file to contain all the class information and implementation. Thus controlling class interaction is very different between Objective-C and Java.

Header files, for those of use who are strict Java programmers, are the public interface for your classes. They contain all the information on how to use the class, specifying what your class can do for other classes. Implementation classes (.m) hold all the code that makes the class function. Think of the header file as an API the .m file is implementing. 
 
 In an-others words:

“[The header file is] used to separate between the public and private parts of a class. The .m file is the implementation. It is where all the logic goes, all the data is processed and stored, etc… The .h file is the interface of that class. It’s literally like an API for your own class. It tells other classes how to use it and how to interface with it.”

Source: http://stackoverflow.com/questions/17558210/h-and-m-files-in-objective-c#comment25542432_17558210
 answer by Fogmeister

Program Syntax:

In Java:

 //Imports go here
public class MyClass
{
//Global variable are declared here. They can be private or public.
//Constructor(s)
//Method declaration and implementation 
//Getters and Setters for accessible variables
}

In Objective-C:
 .h file:

 //Imports 
@interface MyClass:NSObject
{
//Instance variables are declared here
//Instance variables correspond to actual storage space and cannot be accessed
//ivars.
}
// class properties declared here
//Properties can be accesses and they typically corispond to an ivar and provides the getter/setter functionality.
//Properties have their accessor methods synthizied so the getter and setter never need to be declared.
// class methods and instance methods declared here
//All methods put here are Public methods.
@end

.m file:

 // imports
@implementation MyClass
//Methods are implemented here
// Class/ program logic
// Class methods defined here
@end

Variables:

Objective-C and Java share the same basic data types: char, int, float, double. Keep in mind however that not all Objective-C variables are strongly typed. Global variables are declared in the header file for Objective-C and just under the class declaration in Java.

To declare a public variable:
 (Objective-C code is in the .h file)
 Java:

 public type varName;
public int myInt;

Objective-C

@interface MyClass:NSObject
{
@public type varName;// with the @public tag the ivar can be accessed by outside classes without accessor methods.
@public int myInt;
}
@end

private variables:
 Java:

 private type varName;
private int myInt;

Objective-C

@interface MyClass:NSObject
{
@private type varName;
@private int myInt;
//or
type varName;//Instance variables CANNOT be accessed when declared like this.
int myInt;
}
@property type varName; // the Property tag encapsulates the ivar. It doesn’t make it public it allows access through accessor methods the compiler with synthesize.
@end

Constants:
 Java:

 private static final type VAR_NAME;
private static final int MY_INt = 10;

Objective-C

@interface MyClass:NSObject
{
extern const type VAR_NAME;
extern const int MY_INT;
}
@end

.m file

@implementation MyClass
type const VAR_NAME= <value>;
int const MY_INT = 10;
@end

Strong vs. loosely typed:
 Note that only Objective-C can have variables that can be loosely typed. Java is only a strongly typed language.

 Strong:

@interface MyClass:NSObject
{
@private type *varName; // Note the * that is what declares the variable to be strongly typed.
@private int *myInt;
}
@end

Weak:

@interface MyClass:NSObject
{
@private type varName;
@private int myInt;
}
@end