Sadly, the universe does not run on Python. Next semester I start with Java, but I've also decided I'd like to learn Objective-C. So I purchased Programming in Objective-C, and now I'm slowly learning about all of the things normal programmers have to think about that Python has kept hidden from me.
I'm told that the extra steps are part of what makes Objective-C a powerful language, that (for example) having to define the types of my variables ultimately gives me more control over the code I'm writing. It's hard to see that right now. In other words, in Python I could write something like this to create an object from the class "MyClass" and assign it to the variable mySampleObject:
mySampleObject = MyClass()
So that right there creates a variable mySampleObject, and it allocates memory for the object itself, and it creates the object and initializes it with any default values the class might have laid out, and it points the mySampleObject to the instance of MyClass that it just created. It's also important to remember that mySampleObject isn't actually the object, but just a reference to it.
In fairness, there is actually a lot going on behind the scenes that Python keeps hidden from the programmer. That's really nice, until you try to learn a language that doesn't hold your hand so much.
So as I'm currently understanding Obj-C, and fair warning, this may be wrong or misleading, when I create an object in the normal way, I might say something like this:
MyClass *mySampleObject = [[MyClass alloc] init];
and this accomplishes the same thing as the the Python code that precedes it. Except it's not quite the same thing. In Python, if i wanted to point the mySampleObject variable to something else, I can. I can even point it to a totally different type of data, like a string or an integer. Not so with Objective C. With my code above, mySampleObject must always point to an object that is an instance of MyClass. I can't change it to a different class or make it point to an integer.
So then I guess Objective-C has a datatype called "id" which can point to any kind of object. But I'm not going to learn about that until chapter 9 :(
No comments:
Post a Comment