Archive for category Objective-C

When the Deployment target is set lower than the specified SDK

How can you easily take advantage of classes that exist in the SDK your linking against but don’t exist in the minimum OS level you specify to support. This has just been made easier by taking advantage of weak linking. This new feature was added in 10.7, demonstrated at WWDC 2011 and works down to 10.6.8.

Read the rest of this entry »

Tags: ,

Private members & properties declared in class extension or implementation plus method order

This was added in Xcode 4.3 and works down to 10.6.8 I believe.

The aim of course is to have a concise public interface for a class that exposes only the necessary API (interface) for using the class as necessary. This however was not possible as all member variables and properties had to be declared in the public header file. With the update to Objective-C member variables and properties can be moved into a class extension or into the implementation section of the class.

Read the rest of this entry »

Tags: , , , ,

Commander

I’ve been working on Commander, and the first tiny bit of usable functionality is now working.

Update: 27/06/2013 – I’ve uploaded the Commander application: Commander App

The project is on git hub and can be viewed at: https://github.com/SheffieldKevin/Commander

The first bit done is the command line tool which exports movies. The movie exporter command line tool is included in the Commander application package. The first Commander window implemented which can be accessed from the Commands menu is Export Movie… and provides a UI for configuring all the options for the movie export command line tool. My next task will be a way to configure options for the frame grabber command line tool. The frame grabber command line tool can be built using the project but it is currently not built into the Commander application. The Commander application is not codesigned.

For information about which various bits of cocoa and objective-c have been used see over…

Read the rest of this entry »

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.

 

Tags:

Learning Blocks

Much of this content is straight from Apple documentation though at times rephrased to capture how I think about it. Some things are left out, usually only when I’m more than comfortable with the content or that I can’t see myself using certain functionality and therefore I don’t feel the need to rehash.

A block is like a function that can also make use of variables that exist in the same scope that the Block is defined.

Read the rest of this entry »

Learning ARC

Automatic Reference Counting (ARC) is a memory management feature for managing Objective-C objects. From Apple we first had manual reference counting which leaves the coder with the responsibility of managing Objective-C objects. Apple decided this was unsatisfactory and tried garbage collection and garbage collection proved to be unsatisfactory, so Apple replaced it with ARC. I don’t think ARC is the perfect solution to memory management but I think it is good enough for it to be Objective-c’s long term solution to memory management.

This is basically my notes as I try and learn ARC. For those of you familiar with Apple’s documentation much of this will be recognizable.

An alternative tutorial for ARC is this ARC tutorial by Mike Ash. This blog post by Mugunth Kumar is also good.

Read the rest of this entry »

Tags: