Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are different ways to do that. Two common methods is to use a global <code>NSString</code> constant or a preprocessor <code>#define</code> directive.</p> <h2>Global constant</h2> <p>A popular approach is to use a global variable. You need to add it to some file. It could be an existing file or a separate file. Make sure that it's outside the <code>@implementation</code> section if it exists. It could look something like this:</p> <pre><code>NSString *const MyStringConstantIdentifier = @"UD_GPS_LAST_UPDATE"; </code></pre> <p>Then add the same identifier with the <code>extern</code> attribute to a header file which you include in all source files where you want to use the string constant.</p> <pre><code>extern NSString *const MyStringConstantIdentifier; </code></pre> <p>Now <code>MyStringConstantIdentifier</code> will refer to the same string in all places where it's used.</p> <pre><code>[[NSUserDefaults standardUserDefaults] objectForKey:MyStringConstantIdentifier]; </code></pre> <h2>Preprocessor directive</h2> <p>Another approach is to use a preprocessor <code>#define</code> directive in a header file. Make sure that you include the header file in all source files where you want to use the identifier.</p> <pre><code>#define MyStringConstantIdentifier @"UD_GPS_LAST_UPDATE" </code></pre> <p>Now when you include that header file <code>MyStringConstantIdentifier</code> will be available as a shortcut for writing <code>@"UD_GPS_LAST_UPDATE"</code>. This will however put the burden on the preprocessor rahter than the compiler. The difference from using a global variable is that when you use</p> <pre><code>[[NSUserDefaults standardUserDefaults] objectForKey:MyStringConstantIdentifier]; </code></pre> <p>the preprocessor will actually substitute <code>MyStringConstantIdentifier</code> with <code>@"UD_GPS_LAST_UPDATE"</code> so that the code that the compiler processes looks like this:</p> <pre><code>[[NSUserDefaults standardUserDefaults] objectForKey:@"UD_GPS_LAST_UPDATE"]; </code></pre> <p>Where this can be a problem is if parts of your code ever moves into a library. Because preprocessing happens at (actually just before depending on how you look at it) compile time the substitution will replace the constant with the string at all places where it is used. Let's say that this is defined in a library. Whenever the string is changed in the library any application which uses it will have to be recompiled.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload