Well, iam now over half-way through the “Learning Objective-C” book from Apress. Might i add, im loving Objective-C! It is an elegant language (if not hard to grasp at first), and makes learning it a pleasure!.
Anyway, what have i learnt. Well, alot of the OOP theory i already knew, and to be fair most OOP is the same through languages (that support OOP!). Now what does Objective-C offer from an OOP point-of-view:
*Inheritance - NOT Multiple Inheritance
Now before i move on, some people have asked me “What is Composition?”. I have been aware of this through C++, but you may already be aware of it without knowing it! The .NET Framework uses it extensively.
Composition is where you create a class with pointer references to other class objects as instance variables. You are composing objects of other objects, and not ANSI standard (int, float, char). For e.g.:
@interface ipcCar : NSObject
@end // ipcCar
Composition CAN be used with inheritance, as you can see from above. So, composition is nice! So, did you know it already? See programming isnt hard! In addition to Composition, i can learnt some more about Xcode, which is good IDE to get to grips with.
Now any one who knows me, knows that i like to know what is happening under the hood of these framework classes im using. Microsoft’s .NET Framework has revolutionized how we code. It has given us an extensive range of classes to use; inherit and mould, given us easy reflection runtime functionality curtsy of Just-In-TIme compiler and CLR (Or MS take CLI). But what did we do before .NET? Programmers had to UNDERSTAND how code works and WHY. Yes .NET lets us make quick; robust programs, but it takes away the knowing why we are doing things. Take for e.g. the MFC (Microsoft Foundation Class) used by C++. Before .NET you had to draw your windows for your GUI yourself; understand windows messaging queues and how your app talked to Windows OS itself, and even alittle Driver knowledge thrown in. Being a .NET programmer (most of them anyway), this knowledge is lost. The CLR copes with this for you, and thus we create the “Lazy Programmer”.
Sorry for going on about M$ .NET, we are an Apple iPhone developer after all. So why have i ranted. Well, Apple quite rightly have put restrictions on the amount of memory we use and how quickly our Apps close when the OS asks it to (incoming phone call for e.g.). Now the Lazy Programmer paradigm doesnt work with iPhone development. Memory Management is crucial, and Cocoa deals with memory differently, and we as programmers HAVE TO UNDERSTAND HOW it works, otherwise bugs and leaked memory will occur and your program will fail Apples stringent tests.
So how does Cocoa handle memory. Well the NSObject class (which ALL your classes will be related to, like in .NET ALL your classes will have a relationship to Object), has some nifty stuff. Each time you instantiate an object using new, alloc or copy methods, Cocoa sets something called retain counter to 1 (integer). You have the following class methods to interrogate this counter:
- (id) retain; //increase retain count by 1
- (void) release; //decrease retain count by 1
-(unsigned) retainCount //returns the current value of retain count
When the retain count is 0, Cocoa will call the method dealloc and destroy the object and free the memory. Now there is a class NSAutoReleasePool (subclass of NSMutableArray) that handles releasing of memory cleanly for you. Now Apple suggests you use the retain and release methods instead of this, and over the weekend, im going to give some tips on how to do this, explaining on how to use correctly.
Also remember at the moment you cannot use the garbage collection now available, so we now need to be Smart Programmers and not Lazy! Until then, happy coding.
Over the coming days i will be writing daily posts updating you all on my journey to becoming an iPhone Developer. Now, i have started at the logical point in my view, Learning Objective-C. I have purchased myself a copy of “Learning Objective-C on the Mac” by Apress and will be slugging through the chapters, the finishing point culminating in an (hopefully), successful iPhone App. I have my first App drawn up, but more on that on another day.
So, Objective-C (ObjC). Being from a C++ background, this language is both easy to grasp, but a strange new world (but exciting). The book i have purchased is easy to read but explains in chunks what is going on behind the scenes. ObjC is an extension of C itself with more C++ style OOP structure to it. So far so good i hear you say. Well, it does get alittle confusing as you start delving a little deeper into the language. Take for example square brackets . In other programming languages, these are used to to help you declare an array of an object or reference it by its index, for e.g.:
myString[strlen(myString) - 1] = ‘\n’
This functionality still exists with ObjC, BUT the famous brackets are now used for other functionality, i.e. sending messages and calling functions. Now this is where my C++ brain got (and still is alittle) wracked. In C++ you would call a function (or method in ObjC) like:
YesNo = primeNumber(myNumber);
Now you can still achieve this in ObjC for normal functions/methods. It is when you start dealing with struct & class that things get alittle peculiar. In C++ you have private and public data types (or variables) associated with your class. In ObjC you dont, but you still have methods to access and populate them.
myBox = new Cbox(); //C++ instantiation of a new object - memory allocated via constructor
myBox = [Cbox new]; //ObjC instantiation of a new object - memory allocated via message new
Then you could have a function to set colour:
[myBox SetColour: Red]; //ObjC
This is where the confusion sets in and you need to train your brain syn-tactically(see what ive done there!), in the ObjC way. I have only touched on the surface if ObjC, but it is a wonderful language to start learning. I havent posted much yet, because im only on Chapter 5, but tomorrows post should be alot more jampacked with tips. Later on in the month, i will also give some tips and advice on XCode, Apples excellent take on an IDE (Visual Studio watch out). Apple have also created Cocoa, the Mac/Iphone Framework using ObjC, and again, i hope to explore this in more detail.