{"id":2023,"date":"2014-11-12T15:06:14","date_gmt":"2014-11-12T23:06:14","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=2023"},"modified":"2014-11-23T10:20:16","modified_gmt":"2014-11-23T18:20:16","slug":"2023","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=2023","title":{"rendered":"An example of using Enums"},"content":{"rendered":"<p>After I posted my <a href=\"http:\/\/www.wellgolly.com\/?p=1943\">deviceType code<\/a> on <a href=\"http:\/\/stackoverflow.com\/questions\/26408568\/defining-greater-than-for-a-iphone-6-screen-size\/26408851#26408851\">Stackoverflow<\/a>, 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\u2019t 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\u2019t 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\u2019s the code rewritten as an enum. I prefixed the values with LF but you should use whatever is appropriate for your project.<\/p>\n<p>This is in my header file<br \/>\n<pre><code class=\"preserve-code-formatting\">\n\/\/ Devices as of Fall 2014\ntypedef NS_ENUM(NSInteger, LFdeviceType) {\n&nbsp;&nbsp;&nbsp;&nbsp;LFDeviceTypePhoneClassic,\n&nbsp;&nbsp;&nbsp;&nbsp;LFDeviceTypePhoneRetina3_5,\n&nbsp;&nbsp;&nbsp;&nbsp;LFDeviceTypePhoneRetina4,\n&nbsp;&nbsp;&nbsp;&nbsp;LFDeviceTypePhone6,\n&nbsp;&nbsp;&nbsp;&nbsp;LFDeviceTypePhone6Plus,\n&nbsp;&nbsp;&nbsp;&nbsp;LFDeviceTypePadClassic,\n&nbsp;&nbsp;&nbsp;&nbsp;LFDeviceTypePadRetina,\n};<\/code><\/pre><br \/>\nAnd this is in my .m file.<\/p>\n<p><pre><code class=\"\u2019smaller\u2019 preserve-code-formatting\">\n+ (NSInteger)deviceType {\n&nbsp;&nbsp;&nbsp;&nbsp;CGSize screenSize = [[UIScreen mainScreen] bounds].size;\n&nbsp;&nbsp;&nbsp;&nbsp;CGFloat deviceScale = [UIScreen mainScreen].scale;\n&nbsp;&nbsp;&nbsp;&nbsp;LFdeviceType device = LFDeviceTypePhoneClassic;\n\n&nbsp;&nbsp;&nbsp;&nbsp;if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;device = LFDeviceTypePhoneClassic; \/\/ Just in case it doesn&#039;t make it through the conditionals\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Classic has a resolution of 480 \u00d7 320\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if( (screenSize.height == 480 || screenSize.width == 480) &amp;&amp; deviceScale == 1.0f ) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;device = LFDeviceTypePhoneClassic;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Retina has a resolution of 960 \u00d7 640\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else if( (screenSize.height == 480 || screenSize.width == 480) &amp;&amp; deviceScale == 2.0f ) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;device = LFDeviceTypePhoneRetina3_5;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Retina 4&quot; has a resolution of 1136 x 640\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else if (screenSize.height == 568 || screenSize.width == 568 ) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;device = LFDeviceTypePhoneRetina4;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ iPhone 6 has a resolution of 1334 by 750\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else if (screenSize.height == 667 || screenSize.width == 667 ) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;device = LFDeviceTypePhone6;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ iPhone 6 Plus has an actual size of 2208\u2009\u00d7\u20091242 and resolution of 1920 by 1080\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Reported size is 736 x 414\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else if (screenSize.height == 736 || screenSize.width == 736 ) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;device = LFDeviceTypePhone6Plus;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;} else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;device = LFDeviceTypePadClassic; \/\/ Just in case it doesn&#039;t make it through the conditionals\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(deviceScale == 1.0f) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;device = LFDeviceTypePadClassic;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else if (deviceScale == 2.0f) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;device = LFDeviceTypePadRetina;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/NSLog(@&quot;The device is %@ scale is %f and the height is %f and width is %f&quot;, device, deviceScale, screenSize.height, screenSize.width);\n\n&nbsp;&nbsp;&nbsp;&nbsp;return device;\n}<\/code><\/pre><br \/>\nCall it like this:<br \/>\n<pre><code class=\"preserve-code-formatting\">\nif ( (&nbsp;&nbsp; [Utilities deviceType] == LFDeviceTypePhoneClassic \n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|| [Utilities deviceType] == LFDeviceTypePhoneRetina3_5) &amp;&amp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;numberOfFoilsOnScreen &gt; 7 ) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;numberOfFoilsOnScreen = 7;\n}<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\u2019t know how useful they were. I did some research on enums and they are nice. They make the code a bit more &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=2023\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">An example of using Enums<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-2023","post","type-post","status-publish","format-standard","hentry","category-ios"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2023","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2023"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2023\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2023"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2023"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2023"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}