Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A slight modification of the suggestion of @Krizz, so that it works properly if the constants header file is to be included in the PCH, which is rather normal. Since the original is imported into the PCH, it won't reload it into the <code>.m</code> file and thus you get no symbols and the linker is unhappy.</p> <p>However, the following modification allows it to work. It's a bit convoluted, but it works.</p> <p>You'll need <strong>3</strong> files, <code>.h</code> file which has the constant definitions, the <code>.h</code> file and the <code>.m</code> file, I'll use <code>ConstantList.h</code>, <code>Constants.h</code> and <code>Constants.m</code>, respectively. the contents of <code>Constants.h</code> are simply:</p> <pre><code>// Constants.h #define STR_CONST(name, value) extern NSString* const name #include "ConstantList.h" </code></pre> <p>and the <code>Constants.m</code> file looks like:</p> <pre><code>// Constants.m #ifdef STR_CONST #undef STR_CONST #endif #define STR_CONST(name, value) NSString* const name = @ value #include "ConstantList.h" </code></pre> <p>Finally, the <code>ConstantList.h</code> file has the actual declarations in it and that is all:</p> <pre><code>// ConstantList.h STR_CONST(kMyConstant, "Value"); … </code></pre> <p><strong>A couple of things to note:</strong></p> <ol> <li><p>I had to redefine the macro in the <code>.m</code> file <strong>after</strong> <code>#undef</code>ing it for the macro to be used.</p></li> <li><p>I also had to use <strong><code>#include</code></strong> instead of <code>#import</code> for this to work properly and avoid the compiler seeing the previously precompiled values.</p></li> <li><p>This will require a recompile of your PCH (and probably the entire project) whenever any values are changed, which is not the case if they are separated (and duplicated) as normal.</p></li> </ol> <p>Hope that is helpful for someone.</p>
    singulars
    1. This table or related slice is empty.
    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. 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.
 

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