Having spent the last 4 days in ANSI C and getting as far as creating structs and dabbling with function pointers the move to Objective C was fantastic. We were shown comprehensively how Objective C is really only C with an added layer of syntax which allows us to deal with the unwieldy (though evidently powerful) structs and functions we learned in C. This time calling “functions” that relate to those “structures” involves nothing less than sending a message to an object.
[Object Message]
We raced through the entire scope of the language features in about an hour then delved back into Xcode to start writing.
On our first day we hit the ground running, creating two different classes of objects with their inherent data structure and functions (we had Persons (with data firstname, lastname, age etc. who could laugh and display their data) and Dogs who really could just bark, laugh or display their data). Having done the C part we were very comfortable creating the data structures and methods for each of the Classes of Objects.
I thought things were going to go beyond me when by lunchtime Jonathan, the trainer, was setting up an array of a mixture of these objects:
NSArray *array = [NSArray arrayWithObjects:fred,
[[Dog alloc] init],
[[Person alloc] initWithName:@”Sue” age:45],
[[Dog alloc] init], nil];
Already in C I would have been bogged down in the debugger but we were shown the beauty and power of ObjC as we looped through the objects in the array asking each Object to “display” itself and do what it means by “laugh”.
for(id currObj in array)
{
if([currObj conformsToProtocol:@protocol (Laughing)])
[currObj display];
[currObj laugh];
}
Clearly depending on the object a Person does that differently to a Dog. This concept immediately clicked with me in terms of how Objects are used in the UI. My software has no idea what I’m going to throw at it. If I ask my software to cut something (imagine a collection of mp3, image, text in a file) this process shows that my software can easily determine the methods permissible in each type and doesn’t really need to know how they do it but just asks them all to “Cut” themselves, however that happens.
We added a further level of power by creating protocols and testing each object for its conformity to a set of protocols (ie. methods or “functions” that are shared among the various classes of objects – Dogs and persons all can laugh and display themselves). This meant that we weren’t asking objects to “laugh” that had no method of “laughing”.
The obscure “self” and “super” I had seen in the various ObjC books I had attempted to interpret for myself now also made complete sense.
I’d say the benefit of the course to me has been that I understand every line of the code above and more than that I appreciate what is going on behind the scenes in terms of the underlying C structures and functions and the position of each object in memory. Understanding is a wonderful thing.
Day 2 (a Saturday wow!) starts in 90 minutes, Looking forward to it then one day’s sightseeing and back on Monday for 5 days straight into iPhone. The guys from Flipside 5 are coming in to assist Jonathan with that.
Max O’Malley – seeing the beauty in the logos