What is dependency injection 

A simple definition is:

Dependency Injection is where components are given their dependencies through their constructors, methods, or directly into fields.

For example a class constructor creates an instance of an object. This class is now dependant on that object. If however this object is passed into the constructor this dependancy is decoupled.

For dependancy injection to work correctly each class requires an interface. This will ensure if the object is switched it will still present the same methods, it doesn’t however need to know what these methods do.

Unit testing as one of the main uses for dependancy injection. It allows objects which perform complex data processing tasks to be replaced with an object which provides mock data for testing. The class being tested doesn’t care how this data is created as long as it’s presented in the same format.

Another example is an object provided SQL database access methods. Using dependancy injection we can switch this to another object with MySQL access methods. As long as both objects expose the same methods the objects using them won’t care which one it is.

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