Posts Tagged code objective c

Experimental ARC

To help better understand Automatic Reference Counting (ARC) I thought I’d write a collection of routines that highlight what happens in various different situations. I am taking advantage of the fact that a weak pointer becomes nil when the object it points to is deallocated.

The rule that the compiler is keeping to is to make sure that any object that is pointed to by a strong pointer will not be deallocated, and once the object no longer has any strong pointers pointing to it, it will be deallocated soon after. How this is achieved is an implementation detail and many of the routines below highlight different aspects of how the implementation is achieved. As such you should never rely on any object not being deallocated because you believe it will not be deallocated because it is still retained within an autoreleasepool as is demonstrated in the following code. This is important because how the goal is achieved of keeping an object alive over return boundaries is an implementation detail and the implementation can change in the future. For blocks the behaviour will be unreliable as you don’t what is between your code implementing the blocks and where the block gets called, there could for instance be one or more autoreleasepools.

One of the main things I got out of doing this is a better understanding of why I need to use autoreleasepools.

Can you work out what the last NSLog line for each routine will output? The answers are at the end.

One thing to note before reading the code and trying to determine what the output is, is that passing a weak pointer to a function (NSLog for example) will result in the object pointed to by the weak pointer being retained and put into the autoreleasepool. Also if “%@” gets a nil string it will output “(null)”.

Read the rest of this entry »

Tags: , ,

NSArray, NSDictionary and NSNumber literals

You can now initialize NSDictionary, NSArray and NSNumber with literals like you have been able to do with NSString.
For example:

NSArray *array = @[@"string1", @"string2", @"string3"];
NSDictionary *dictionary = @{@"key1" : @"object1", @"key2" : @"object2", @"key3" : @"object3" };
NSNumber *intNum = @12345;
NSNumber *uIntNum = @12345ul; // unsigned long
NSNumber *fNum = @123.45f; // floating point.
NSNumber *boolNum = @YES;

You can also access arrays and dictionaries using the [] operator, for example accessing the
NSString item at index 1 you can do the following:

NSString *secondString = array[1]; // equiv to [array objectAtIndex:1];

Or you can access the string with a key from a NSDictionary:

NSString *stringWithKey1 = dictionary[@"key1"];

Literals are created as autoreleased objects.

For more details see:

Objective-C literals for NSDictionary, NSArray, and NSNumber
Objective-C’s new NSNumber, NSArray and NSDictionary syntaxes mean less typing
Objective-C literals: Big Nerd Ranch, Part 1.
Objective-C literals: Big Nerd Ranch, Part 2.

You cannot have NSArrary or NSDictionary literal initializers not in a function or method, like at the very top of a source file.