b2cloud

    2nd March 2014

    64 bit iOS App Tips

    Tutorial By 3 years ago

    The new iPhone 5S, iPad Air and iPad Mini all now use Apple’s A7 64 bit processor. Just like the introduction of the 64 bit Intel Macs, developers need to update their apps for 64 bit compatibility. If your app was coded well, this means minimal effort and not requiring many code changes.

    First of all, check out Apple’s official 64 bit guide. It provides a lot of great information, however I think there’s a few things missed.

    After updating a couple of my apps, I collected a few tips I think may help other developers also making their apps compatible with 64 bit.

    Enums and options

    Back when iOS6 came out there was a new way of writing enums. The old traditional way still does work, however it leaves the enum type ambiguous. It’s best practice to explicitly set the enum type with the newish NS_ENUM and NS_OPTIONS syntax:

    //	Old (not strictly typed)
    typedef enum
    {
    	B2CategoryDeveloper,
    	B2CategoryDesigner,
    	B2CategoryMarketing
    }
    B2Category;
    
    //	New (strictly typed to NSUInteger)
    typedef NS_ENUM(NSUInteger, B2Category)
    {
    	B2CategoryDeveloper,
    	B2CategoryDesigner,
    	B2CategoryMarketing
    };
    //	Old (not strictly typed)
    typedef enum
    {
    	B2OptionNone = 0,
    	B2OptionBold = 1 << 0,
    	B2OptionItalic = 1 << 1,
    	B2OptionUnderlined = 1 << 2
    }
    B2Option;
    
    //	New (strictly typed to NSUInteger)
    typedef NS_OPTIONS(NSUInteger, B2Option)
    {
    	B2OptionNone = 0,
    	B2OptionBold = 1 << 0,
    	B2OptionItalic = 1 << 1,
    	B2OptionUnderlined = 1 << 2
    };

    Writing to Disk

    If a 32 bit app writes 32 bit integers to disk, and then a 64 bit app tries to read them in as 64 bit integers there will be problems. This is an easily missed problem when using the NSUInteger and NSInteger typedefs, which automatically map to either 32 or 64 bit depending on which architecture you’re compiling before. Pretty tricky to debug, if you plan on writing it to disk then explicitly give it a fixed type across all architectures.

    Format strings

    Format strings differ between architectures.

    You may be tempted to write the following:

    const NSUInteger count = 7;
    
    #ifdef __LP64__
    [NSString stringWithFormat:@"%ld people", count];
    #else
    [NSString stringWithFormat:@"%d people", count];
    #endif

    Of course, this means doubling up on code and get quite messy when there’s a lot of this going on.

    Apple does provide a header you can import to get the correct format string type, #include <inttypes.h>, however if I’m writing Objective-C code I prefer to convert the value to an NSNumber using a boxed expression, which I think looks cleaner.

    const NSUInteger count = 7;
    [NSString stringWithFormat:@"%@ people", @(count)];

    There is a slight performance overhead in converting to an NSNumber and having it parsed in the format string, however for most cases the time is negligible.

    • Another tip that I like to use: when working with CGFloat values, I used to use the float variants of the math.h functions: fabsf, cosf, etc. However, on 64-bit, CGFloat is a double, not a float, so with some compiler settings you will get a warning about losing precision. Now, I always use fabs, cos, and the rest of the functions that take doubles instead of floats. Technically a memory hit, but generally not a problem in common use.

      • Tom

        If you are also building for 32 bit architecture as well, you could do a fancy ifdef/define that will call the float version when compiling for 32 bit, and the double version when compiling for 64 bit. I’m actually surprised Apple hasn’t done this for us already.

    • Hemant Bosamia

      One more tip, sometimes 64 bit does not show images. After lot of research I found it is due to a ViewController included by default in the app. In .m file of that controller just comment the following code

      /*

      @implementation UIImageView (UIScrollView)
      – (void)setAlpha:(float)alpha {
      —— Some code, does not make differecne——
      [super setAlpha:alpha];
      }
      @end
      */

    • Don’t ever compare NSNumber with == operator. Always use isEqualToNumber. It was working fine with == for 32 bit architecture and hell broke down with 64 bit architecture. Another one to keep in mind.

    Recommended Posts

    iOS performSelector with multiple parameters

    Post by 3 years ago

    On iOS the built in convenience method performSelector method call only allows for up to 2 parameters. - (void) aMethod { [self performSelector:@selector(doSomethingWithObject:otherObject:) withObject:@(1) withObject:@(2)]; } - (void) doSomethingWithObject:(id) object otherObject:(id) otherObject { // Code

    Got an idea?

    We help entrepreneurs, organizations and established brands from around
    the country bring ideas to life. We would love to hear from you!