Objective-C for Absolute Beginners by Apress is great in some areas and horrible in others. It spent almost 4 pages on Boolean Logic (easy stuff) and spent 1/2 page on the code bellow.
1. The first line is the METHOD with a return type 'id'? The book says id is a generic object. It looks like it is INITIALIZING this generic object with 2 parameters, newName and newFreq. But I don;t see it ALLOC any memory for this new object to store it?
2. Second line - self = [super init]; The book says that this is common stuff and super is short for super class, but I don't understand what it is doing?
3. line 3 I get, I think. The IF statement seems to be checking if self was created or not. Then assigning name and freq with newname and newfreq.
4. It then returns self?
I am taking a stab in the dark here. This Method is creating a new generic object that contains 2 items (or methods) that are 'NewName' and 'New Frequency'. The return self means that once this method received a message it is returning an object (not from a class, but from this method) that can be used.
Am I close in understanding it?
-Lars
Code:
- (id)initWithName:(NSString *)newName atFrequency:(double)newFreq {
self = [super init];
if (self != nil) {
name = newName;
frequency = newFrequency;
}
return self;
}
1. The first line is the METHOD with a return type 'id'? The book says id is a generic object. It looks like it is INITIALIZING this generic object with 2 parameters, newName and newFreq. But I don;t see it ALLOC any memory for this new object to store it?
2. Second line - self = [super init]; The book says that this is common stuff and super is short for super class, but I don't understand what it is doing?
3. line 3 I get, I think. The IF statement seems to be checking if self was created or not. Then assigning name and freq with newname and newfreq.
4. It then returns self?
I am taking a stab in the dark here. This Method is creating a new generic object that contains 2 items (or methods) that are 'NewName' and 'New Frequency'. The return self means that once this method received a message it is returning an object (not from a class, but from this method) that can be used.
Am I close in understanding it?
-Lars