Instances and Methods: iPhone Programming


door
My skill level jumped up by leaps and bounds through a few guided reviews of my code with a friend. Here’s what I learned, for posterity and retaining my own lesson, ha.

Objective-C has C-styled instance variable initialization and assignment

It may be confusing, but I kept seeing code examples that had this:

NSArray *imageArray = [NSArray arrayWithObjects:@"sample1.jpg",@"noframes.gif"];

// or

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 300.0)];

What it’s actually saying is (broken down to be explicit):

NSArray *imageArray;

That “*” is a pointer, of class “NSArray.” But no memory has been allocated, and it’s not ready for readable nor write-able use. It’s unallocated, uninitialized, but prepped in name only.


NSArray imageArray = [[NSArray init] alloc];

This line is collapsing two functions in one line. This bit = “NSArray init”, is initializing the array, which is copying it from the base class NSArray. The doc says it “prepares it for use.” Which I’m assuming is readability and write-ability.

It’s purely style points to put NSArray in the very beginning of the line- to be explicit, which some are picky about.

The “alloc” is for allocating. The docs say allocation is to “produce new objects,” which I’m thinking is the memory space to expand versions of itself (and descendents). Basically, copy. Like, we want a new Array, based on how NSArray works, so copy NSArray with the new name “myArray” and give it all the bells and whistles NSArray has. Initialization- give the values some space, populate the values or contents of the new variable.

There are some shortcuts- using “initWith..” commands to load up your new copied Class object with values or properties. Also, in shorthand lots of folks put it all on one line the pointer and the initialization, and allocation. Since I was getting confused I broke it down.

In Ruby

Ah, the simple (but powerful!) world of Ruby.

Array.new(5, "A")

Passing a method, “new” to the class type, and various element data types defined by the pass value. Of course, this covers (masks, or hides?) the guts of the Ruby framework that are doing quite a bit more.

In Perl

Declare and assign in one go, strings defined by quotes, the @ sign defines the object type.

my @myArray = ("value1", "value2");

Java

Java defines the element data type “int”, and the object type by the “[]”. You can also use “new” and the size of the array.

int[] anArray; // declares an array of integers

// or

anArray = new int[10]; // allocates memory for 10 integers

When things get wacky

What confuses me is when Objective-C is not strict regarding data types and pointers:

int y=0;

// and

id myRectangle;
myRectangle = [Rectangle alloc];

My friend Patrick was like – you know why this is, right? And I’m like, well, I had a hunch. Which he ended up confirming- that these “variables” are instances of classes in the compiler. So when they are complex, such as NSArray, there are more layers to go through figuratively, at least, and thus require more allocation and initialization. But when you’re closer to the base layer- like with “id” and “int”- thus closer to the NSObject (the class form which instance variables inherit) there is less need to allocate and point, etc. More comments/info are welcome. Here’s Apple’s offiical words on the matter:

Creating Instances

A principal function of a class object is to create new instances. This code tells the Rectangle class to create a new Rectangle instance and assign it to the myRectangle variable:

id myRectangle;

myRectangle = [Rectangle alloc];

The alloc method dynamically allocates memory for the new object’s instance variables and initializes them all to 0—all, that is, except the isa variable that connects the new instance to its class. For an object to be useful, it generally needs to be more completely initialized. That’s the function of an init method. Initialization typically follows immediately after allocation:

myRectangle = [[Rectangle alloc] init];

This line of code, or one like it, would be necessary before myRectangle could receive any of the messages that were illustrated in previous examples in this chapter. The alloc method returns a new instance and that instance performs an init method to set its initial state. Every class object has at least one method (like alloc) that enables it to produce new objects, and every instance has at least one method (like init) that prepares it for use. Initialization methods often take arguments to allow particular values to be passed and have keywords to label the arguments (initWithPosition:size:, for example, is a method that might initialize a new Rectangle instance), but they all begin with “init”.

From “Mac Dev Center: The Objective-C Programming Language: Objects, Classes and Messaging”

,