Thursday, August 2, 2012

[Objective-C beginLearning];

Contrary to whatever I ranted about here, I have in fact decided that it is in my best interest to learn Objective-C.

Objective-C (ObjC) is a language used almost exclusively by Apple for OSX and iOS application development, namely using the Cocoa framework. In relative terms:

ObjC:Apple :: VC++:Microsoft ; Cocoa:OSX :: WinAPI:Windows

The obvious advantage here is that both ObjC and VC++ are derived directly from C.

Imagine that you know and understand Latin. You want to learn both English and Spanish. You know going in that this is not a huge deal, because both come directly from Latin. You also know that English is a lot more complicated than Spanish - Spanish has things like endings, gender, conjugations, and declensions just like Latin does, whereas English boasts none of these amenities.

Now replace "Latin" with "C." You know C. You probably also know C++. You can learn VC++ (let's say it's like Spanish) with relative ease. The syntax is about the same, but the vocabulary is different and also more expansive. Objective-C, then, is English: a very complicated but extremely useful language that takes a long time to learn and even longer to master.

A Chinese guy learning English will struggle with it for some time because it's so radically different from his native tongue. However, after several months of disciplined study, he can probably hold a basic but fully-coherent conversation with a native English speaker.

Similarly, learning Objective-C seems like something where I'll be able to soon write a simple program like GJSieve, but will have next to no idea about the intricacies and complexities of the language and will therefore be unable to take advantage of all the language has to offer.

People say that Macs are simplistic, and after only a day of learning the language behind Mac programs, I have to say they're wrong.

It seems to be a widely-held opinion amongst professional programmers that "Objective-C is needlessly complex" - but those people will tell you in the same breath that this, more often than not, means there is only one way to do things. This means that if you write this:

@interface doStuff : NSObject {

IBOutlet NSTextView *TextPlace

@property(nonatomic,retain) IBOutlet id TextPlace

- (IBAction)doThatThing:(id)sender;


then there is only really one way to properly implement it:

@implementation doStuff
@synthesize TextPlace;

- (IBAction)doThatThing:(id)sender
NSTextStorage *TextStorage = [TextPlace textStorage];
NSString* String = [NSString stringWithFormat: @"It did stuff."];
[TextStorage beginEditing]
[TextStorage appendString:String];
[TextStorage endEditing];

You should note that this example wouldn't really work. I mean, it might, but then again it might need a few minor changes...

The point is, Objective-C thrives on interface and implementation. Much like the Windows API, there are 3 steps to getting things to do stuff in Cocoa:
  1. define the way the thing is supposed to behave
  2. define how the thing does stuff when it behaves
  3. link the thing to a button or some other control that tells it to start doing stuff
The "thing," of course, is a class (or "object.")

I had never noticed how convenient these are. It's fantastic, actually. I make a separate file for each button, and while that takes up space, it also means no more hunting around one giant file!

You want a button that displays a window? Okay cool.
  1. Make a window in the Interface Builder (compare to Windows' Resource Editor)
  2. Make a button in the main window, also in IB
  3. Make an ObjC class that's a subclass of NSObject
  4. Tell that class the window and the button you made are "outlets"
  5. Tell that class to show the window when the action message is received
  6. In IB, link the action message and outlet pointer to the button and the other outlet to the window
  7. go
Obviously steps 3-5 take a bit more work, but I'm not ready to share my code yet. I mean, there's not much to share!

So, I'll leave you with this to showcase my (limited) achievements in Objective-C thus far (click for full view):

No comments:

Post a Comment