<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Objective-C Macro on Andrew Bancroft</title>
    <link>https://www.andrewcbancroft.com/tags/objective-c-macro/</link>
    <description>Recent content about iOS development with Swift in Objective-C Macro  from Andrew Bancroft.</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Wed, 01 Oct 2014 17:58:46 +0000</lastBuildDate>
    
        <atom:link href="https://www.andrewcbancroft.com/tags/objective-c-macro/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Swift Alternative to Objective-C Macros</title>
      <link>https://www.andrewcbancroft.com/2014/10/01/swift-alternative-to-objective-c-macros/</link>
      <pubDate>Wed, 01 Oct 2014 17:58:46 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/10/01/swift-alternative-to-objective-c-macros/</guid>
      <description>&lt;p&gt;I&amp;rsquo;ve previously written about &lt;a title=&#34;Replace Magic Strings with Enumerations in Swift&#34; href=&#34;http://www.andrewcbancroft.com/2014/09/02/replace-magic-strings-with-enumerations-in-swift/&#34; target=&#34;_blank&#34;&gt;using enumerations in Swift to encapsulate special values&lt;/a&gt; that could end up falling into the &amp;ldquo;magic string” category if they were to simply be scattered in-line throughout your code. The primary example I proposed for such a &amp;ldquo;magic string” replacement was Storyboard Segue Identifiers.  These special identifiers have such a specific purpose that felt to me like a natural fit to create a Type in the form of an enumeration (which I called SegueIdentifier) to group them all together in one place so that I could easily find them and modify them, should I ever need to do so.&lt;/p&gt;
&lt;p&gt;I still like that solution for &lt;em&gt;groups&lt;/em&gt; of things, but it&amp;rsquo;s a lot of &amp;ldquo;ceremony” to use enumerations for encapsulating &lt;em&gt;everything&lt;/em&gt; that may have been implemented as a macro expression or a static global constant in Objective-C.&lt;/p&gt;
&lt;p&gt;I ran across this in the &lt;a title=&#34;Apple Developer Documentation - Using Swift with Cocoa and Objective-C&#34; href=&#34;https://developer.apple.com/library/ios/documentation/swift/conceptual/buildingcocoaapps/InteractingWithCAPIs.html#//apple_ref/doc/uid/TP40014216-CH8-XID_19&#34; target=&#34;_blank&#34;&gt;Swift developer documentation&lt;/a&gt; that I think will be of help to folks who want to avoid &amp;ldquo;magic values” throughout their code, but don&amp;rsquo;t want to employ enumerations where they&amp;rsquo;re not the best fit.  Here&amp;rsquo;s a snippet:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/09/Macro-Alternatives-in-Swift.png&#34;&gt;&lt;img class=&#34;alignnone size-full wp-image-5231&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/09/Macro-Alternatives-in-Swift.png&#34; alt=&#34;Macro Alternatives in Swift&#34; width=&#34;705&#34; height=&#34;149&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/09/Macro-Alternatives-in-Swift.png 705w, https://www.andrewcbancroft.com/wp-content/uploads/2014/09/Macro-Alternatives-in-Swift-300x63.png 300w&#34; sizes=&#34;(max-width: 705px) 100vw, 705px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;So there you have it, folks!  One easy alternative to your former &lt;code&gt;#define&lt;/code&gt; habits when you&amp;rsquo;re working in Swift is to simply declare a &lt;em&gt;constant&lt;/em&gt; (using the &lt;code&gt;let&lt;/code&gt; keyword) instead.  The word &amp;ldquo;constant” is &lt;em&gt;key&lt;/em&gt; – the last thing you want to do is declare a &lt;em&gt;variable&lt;/em&gt; in some global scope (using the &lt;code&gt;var&lt;/code&gt; keyword) where the value of the identifier could be changed somehow, if even by accident.&lt;/p&gt;
&lt;h3 id=&#34;organizing-define-replacement-constants&#34;&gt;Organizing #define Replacement Constants&lt;/h3&gt;
&lt;p&gt;The question that naturally comes next is, &amp;ldquo;Where do I declare a constant that I&amp;rsquo;m using to replace a #define?”&lt;/p&gt;
&lt;p&gt;The answer is not black and white – a few factors play into your decision of where to declare them.&lt;/p&gt;
&lt;p&gt;In general, I would probably declare such a constant in the location that&amp;rsquo;s closest to the context in which it is used.  Here are a few examples to consider:&lt;/p&gt;
&lt;li style=&#34;text-align: left;&#34;&gt;
  If it&#39;s only going to be used in a single function, it&#39;s reasonable to declare the constant locally at the top of that function, or anywhere near where it will be used.
&lt;/li&gt;
&lt;li style=&#34;text-align: left;&#34;&gt;
  If it&#39;s only used in a single class/struct, perhaps declaring it at the top of that class/struct is a good idea.
&lt;/li&gt;
&lt;li style=&#34;text-align: left;&#34;&gt;
  If it&#39;s going to be a value that&#39;s used in &lt;em&gt;multiple&lt;/em&gt; classes/structs, it may be time to create a new .swift file and place it there so that you can find it again.
&lt;/li&gt;
&lt;li style=&#34;text-align: left;&#34;&gt;
  Start with a very small scope, and as that constant broadens in its usage throughout your project, gradually move it to more and more globally visible locations.
&lt;/li&gt;
&lt;p&gt;Using a globally-defined constant is exactly what I did for &lt;a title=&#34;Swift iOS Version Checking&#34; href=&#34;http://www.andrewcbancroft.com/2014/09/17/swift-ios-version-check/&#34; target=&#34;_blank&#34;&gt;checking the iOS version number of a user&amp;rsquo;s device&lt;/a&gt;.  I simply created a new file called &amp;ldquo;&lt;span style=&#34;color: #404040;&#34;&gt;iOSVersions.swift”, placed my global constant definitions in it, and was able to reference those constant names everywhere in my project that I needed to perform conditional logic based on the iOS version number.  &lt;/span&gt;&lt;/p&gt;
&lt;h3 id=&#34;summary&#34;&gt;Summary&lt;/h3&gt;
&lt;p&gt;A simple Swift alternative to a &lt;span class=&#34;lang:objc decode:true  crayon-inline &#34;&gt;#define&lt;/span&gt; macro in Objective-C it to define a constant at a scope that&amp;rsquo;s appropriate for where you plan to use that constant.&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>