Rich's Mad Rants
Powered by Squarespace

Entries in Literal Syntax (1)


Notes about Xcode 4.5

Here are a few things that may not be immediately obvious about Xcode 4.5

Retina 4 support

So, you have an existing app, and you want to get it ready for the soon-to-be-released iPhone 5. You've already gone through all the work to make sure your views auto-resize properly (using either the old autoresize mask or iOS 6's autolayout features). So, you bring up your project in Xcode. Switch the simulator over to the 4-inch device (in the iOS Simulator's menu: Hardware > Device > iPhone (Retina 4-inch)), and run your app.

Unfortunately, nothing happens. Your app is still displayed with the black bars at the top and bottom.


Ok, the problem is, you don't know the secret handshake. Simply add a retina-4 launch image to the project. This will (magically) enable the full 4-inch mode. You can either add it through the project's Target > Summary settings, or simply add a file named Default-568h.png to the project (remember, the iPhone 5's screen is 568 points tall--which translates into 1136 pixels, hence the name). In either case, the launch image needs to be 640 x 1136 pixels.


Note: Xcode 4.5 will automatically add black launch images to new projects. This means the full 4-inch mode will be enabled by default on any new projects you create.

I'm not a huge fan of this as a design choice. While it's a slick way to automatically check for 4-inch support in legacy projects--there's a little too much magic going on in the background, with no obvious connection between cause and effect. I'd rather have an explicit plist setting that we explicitly needed to toggle on or off. But, that's just me.

Literal Syntax Support

I'll admit it, I'm loving the new literal syntax. Here's a mini review, in case you're not familiar with it.

We've always had literal syntax for strings. To create a new string, we'd simply append the at-sign before a C-style string, as shown below:

NSString *myString = @"this is a literal string"

Now, we have support for literal NSNumbers, NSArrays and NSDictionaries.

NSNumber *aNumber = @42;

NSNumber *anotherNumber = @3.14;

NSNumber *alsoANumber = @YES;

NSNumber *thisEvenWorks = @([self aMethodThatReturnsANumber] + 2.0);

NSArray *array = @[@"First", @"Second", @"Third"];

NSDictionary *dict = @{@"FirstKey":@"I'm the first value",

                      @"SecondKey":@"I'm the second value"};

Apple's also added syntax to support subscript notation for accessing the values in arrays and dictionaries:

NSString *second = array[1];

array[1] = @"I'm the new second element";

NSString *secondValue = dict[@"SecondKey"];

dict[@"SecondKey"] = @"I'm the new second value";

We can even add subscript support to our own objects. We just need to implement any or all of the following methods:

// integer-based getter -- like NSArray

- (id)objectAtIndexedSubscript:(NSUInteger)index

// Object-based getter -- like NSDictionary

- (id)objectForKeyedSubscript:(id)key

// integer-based setter -- like NSArray

- (void)setObject:(id)anObject atIndexedSubscript:(NSUInteger)index

// Object-based setter -- like NSDictionary

- (void)setObject:(id)object forKeyedSubscript:(id < NSCopying >)aKey

Note: We are implementing these methods, not overriding them. This means that a) Xcode's autocomplete will not help us get the method names correct, and b) if you don't declare these methods in your header file, you'll get compilation errors.

We've been able to use the literal syntax for iOS development since Xcode 4.4--but we couldn't use the subscript notation, since the support had not yet been added to the objects themselves. However, now that Xcode 4.5 is out, we can use both. But here's the scary bit. As long as you use the iOS 6.0 SDK (which is the default), you can deploy the project to earlier versions of the OS, and subscript notation still works.

I've tested this on an iOS 5.1 phone. I was able to use subscript notation to get and set values into an NSMutableArray. I was also able to add subscript support to my own objects, and use it without any problems. That's some scary dark voodoo shit, let me tell you.

However, I'm just glad I can use literal syntax and subscripts without worrying about my deployment targets.

Deployment Targets

And one last note about deployment targets. With Xcode 4.5, Apple's dropped support for anything older than iOS 4.3. 

All I can say is, about damn time!