Monday, January 13, 2014

Migrate code to support 64-bit in iOS7

As Apple requires all app submissions to use XCode5 with iOS7 SDK after February 1st, 2014, it becomes important to make sure that your codebase is compatible with iOS7 SDK, especially the 64-bit support since the primitive types change from 32 to 64 bits.

This means that NSInteger/NSUInteger are long instead of int.  CGFloat is double, not float.

There are many possible compiler warnings and here are the resolutions.

Case 1: Implicit conversion loses integer precision: 'long' to 'int'


    NSInteger row = ...;

    int rowWithLineCount = (rows - 1) / 5// compiler warning

    NSInteger rowWithLineCount = (rows - 1) / 5 ;

Note: Use NSUInteger if the right hand expression is evaluated to unsigned integer.

Case 2: Implicit conversion loses integer precision: 'NSInteger' (aka 'long') to SomeEnum'


  typedef enum {
  } SomeEnum;

  NSInteger someValue;
  SomeEnum somefield = someValue; // compiler warning

Solution: Use NS_ENUM

  typedef NS_ENUM(NSInteger, SomeEnum) {

Case 3: Implicit conversion loses integer precision: 'SomeEnum' (aka 'enum SomeEnum') to int'


  typedef NS_OPTIONS(NSUInteger, SomeEnum) {
  SomeEnum somefield;

  NSNumber *number = [NSNumber numberWithInt:self.someField]; // compiler warning


  NSNumber *number = [NSNumber numberWithLong:self.someField];

Case 4: Values of type 'NSInteger' should not be used as format arguments; add an explicit cast to 'long' instead


NSInteger statusCode = ...;

NSLog(@"Failed response code:%d", statusCode); // compiler warning


NSLog(@"Failed response code:%ld", (long)statusCode);

Note: This applies to %lu string pattern and (unsigned long) casting for NSUInteger as well.

Case 5: Replace abs((NSInteger)value) with labs()