Class Vs Instance Methods

– Class Method/Variables

These apply to all objects created from a particular class. For example a class variable called numCarsProduced, updating this variable in one instance of the class would reflect in any other instances created from that class.

– Instance Method/Variables

These are unique to each class ie. the class Car could have a instance variable colour or make. In addition an instance method setMake and setColour would only effect that particular instance of the class.

Objective C Strings

1. Convert NSString to float:

floatValue = [NSString floatValue]

2. Convert NSString to integer:

intValue = [NSString intValue]

3. Convert float to NSString with current float precision xx.xxxxxx by default (i.e. 6.000000):

NSString = [NSString stringWithFormat:@”%f”, returnValue]

4. Convert float to NSString removing trailing zeros (i.e. 6.560000 will be 6.56)

NSString = [NSString stringWithFormat:@”%g”, returnValue]

Defining Method Arguments

When defining a method in Objective C the parameters are given a names. This make the code more readable. Each parameter name is delimited by a : followed by the parameter type and variable.

Techically the name is option but strongly recommended. Both the following examples are valid, but the second version defines a name for each parameter.

-(void)setReecordValues:(NSString*)nTitle :(NSString*)ns1 :(NSString*)ns2 :(NSString*)nNote;

-(void)setReecordValues:(NSString*)nTitle newSecret1:(NSString*)ns1 newSecret2:(NSString*)ns2 newNote:(NSString*)nNote;

The method would then be called in the following way.

[objectname setReecordValues: title newSecret1:s1 newSecret2:s2 newNote:notes]

As you can see by using names its obvious what each parameter is and where its going.

Alloc Command

The most popular class method is alloc, which allocates space for an instance of the class (but does not initialize the class — that is usually reserved for the instance method init). A ‘-‘ indicates an instance method. For example, the doSomethingWith: method defined above takes one argument, the object anotherObject, and returns an integer. Class messages can only be sent to a class. Instance messages can only be sent to an instance of a class.

Example:

newObject = [[NewObject alloc] init]; –create and initialize the object