From Java To Objective-C: String

From Java To Objective C: Strings

My Notes (part 2)


This is a very short introduction from my notes.

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


The String.


In java we have String Class that represent a string, in objective C there is a NSString object.

Each string constant double quote in objective-c must be prefixed by @

Also in objective C, NSString is immutable like java String.



.objective c version


  • NSString aString = @"hello world";

  • NSString aString2 = [[NSString alloc] initWithString:@"Hello World2"]; 
  • [aString release]; 

  • #define CONSTANT1 @"HELLO";

  • NSLog(@"debug string");

  • NSString *ageString = [NSString stringWithFormat:@"%@ concatenate with %@", "age:", 18];


.java version


  • String aString = "hello world";

  • String aString2 = new String("Hello World 2");

  • public static final String CONSTANT1 = "HELLO";

  • System.out.println("debug string");

  • String ageString = "age:" + " concatenate with " + 18;



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 }
}