An example of using Enums

After I posted my deviceType code on Stackoverflow, one of the commenters suggested that I use enums. Now I know what they are because Apple uses them all the time but I didn’t know how useful they were. I did some research on enums and they are nice. They make the code a bit more readable, but mostly they allow the compiler to help you type and catch errors. Xcode will autocomplete your deviceType for you and will give you the error: Use of undeclared identifier if you try to use a value that isn’t defined. I suspect that they are slightly faster in comparisons since they use integers rather than strings. Also, you can use them directly in switch statements. Here’s the code rewritten as an enum. I prefixed the values with LF but you should use whatever is appropriate for your project.

This is in my header file


// Devices as of Fall 2014
typedef NS_ENUM(NSInteger, LFdeviceType) {
    LFDeviceTypePhoneClassic,
    LFDeviceTypePhoneRetina3_5,
    LFDeviceTypePhoneRetina4,
    LFDeviceTypePhone6,
    LFDeviceTypePhone6Plus,
    LFDeviceTypePadClassic,
    LFDeviceTypePadRetina,
};

And this is in my .m file.


+ (NSInteger)deviceType {
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    CGFloat deviceScale = [UIScreen mainScreen].scale;
    LFdeviceType device = LFDeviceTypePhoneClassic;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        device = LFDeviceTypePhoneClassic; // Just in case it doesn't make it through the conditionals
        // Classic has a resolution of 480 × 320
        if( (screenSize.height == 480 || screenSize.width == 480) && deviceScale == 1.0f ) {
            device = LFDeviceTypePhoneClassic;
        // Retina has a resolution of 960 × 640
        } else if( (screenSize.height == 480 || screenSize.width == 480) && deviceScale == 2.0f ) {
            device = LFDeviceTypePhoneRetina3_5;
        // Retina 4" has a resolution of 1136 x 640
        } else if (screenSize.height == 568 || screenSize.width == 568 ) {
            device = LFDeviceTypePhoneRetina4;
        // iPhone 6 has a resolution of 1334 by 750
        } else if (screenSize.height == 667 || screenSize.width == 667 ) {
            device = LFDeviceTypePhone6;
        // iPhone 6 Plus has an actual size of 2208 × 1242 and resolution of 1920 by 1080
        // Reported size is 736 x 414
        } else if (screenSize.height == 736 || screenSize.width == 736 ) {
            device = LFDeviceTypePhone6Plus;
        }

    } else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        device = LFDeviceTypePadClassic; // Just in case it doesn't make it through the conditionals
        if(deviceScale == 1.0f) {
            device = LFDeviceTypePadClassic;
        } else if (deviceScale == 2.0f) {
            device = LFDeviceTypePadRetina;
        }
    }
    //NSLog(@"The device is %@ scale is %f and the height is %f and width is %f", device, deviceScale, screenSize.height, screenSize.width);

    return device;
}

Call it like this:

if ( (   [Utilities deviceType] == LFDeviceTypePhoneClassic 
      || [Utilities deviceType] == LFDeviceTypePhoneRetina3_5) &&
        numberOfFoilsOnScreen > 7 ) {
        numberOfFoilsOnScreen = 7;
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.