Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 3 - Writing Initializers A correct initializer must comply with the following: The initializer name should begin with init The initializer returns.

Similar presentations


Presentation on theme: "Lecture 3 - Writing Initializers A correct initializer must comply with the following: The initializer name should begin with init The initializer returns."— Presentation transcript:

1 Lecture 3 - Writing Initializers A correct initializer must comply with the following: The initializer name should begin with init The initializer returns the initialized object (self) The initializer calls the initializer of the superclass (this will propagate up the chain of inheritance until finally the init() of NSObject is called) The initializer uses the value returned by the superclass init() The initializer must consider and correctly handle construction errors.

2 Method syntax Definition is preceded by + or – and the return type as a C- cast: -(float) result If a method has no parameters, definition and call consist only of the method name Each parameter is represented by a :, followed by the parameter type as a C-cast, followed by the parameter name method1 :(int)par1 Before each colon one can put a label, the labels become part of the method name, compare the two definitions below: -(float)sum:a :b :c -(float)sumofA:a andB:b andC:c

3 example continued Definition Example of call ---------------------------------------------------------------------------------- -(float)sum:a :b :c [ob1 sum :1.1 :2.2 :3.3]; { return a+b+c; } -(float)sumofA:a andB:b andC:c [ob1 sumofA:1.1 andB:2.2 andC:3.3]; { return a+b+c: } The two methods do the same but the second reads much easier. Read the detailed explanations in the lecture notes.

4 Initializers continued Initializer with arguments, example from class HelloWorld -(id)initwithNum:n andValue:v { num = n; value = v; return self; } The big difference is the return self at the end, and the return type of id, this is called dynamic typing. id is the same as void* in C. Now we can write: id hw = [[HelloWorld alloc] initWithNum:3 andValue:3.7]; This initializer is much better but still not totally correct.

5 Initializers continued The call to alloc returns a new object. To this object we should send init by sending the inherited init to the parent class. The initializer should continue only if this init succeeds: -(id)initwithNum:n andValue:v { if (!(self = [super init])) return nil; self->num = n; self->value = v; return self; } The whole call becomes more complicated, but is explained in the lecture.

6 A new class @interface Student : NSObject { NSString* name; float gpa; } -(id)init; -(id)initStudent: (NSString*)name :(float)gpa; @end

7 class Student @implementation Student -(id)init // default initializer { if (!(self = [super init])) return nil; self->name = nil; self->gpa = 0; return self; } -(id)initStudent: (NSString*)name :(float)gpa { if (!(self = [super init])) return nil; self->name = name; self->gpa = gpa; return self; } @end


Download ppt "Lecture 3 - Writing Initializers A correct initializer must comply with the following: The initializer name should begin with init The initializer returns."

Similar presentations


Ads by Google