Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This problem was driving me crazy, but I was able to get <code>NSLocalizedString</code> to behave.</p> <p>zoul was right, if you print the mainBundle to the console in a logic test, it's not the same bundle that contains your Localizable.strings file. You need to conditionally redefine <code>NSLocalizedString</code> whenever you run your unit tests. I did it in the following steps:</p> <ol> <li>We need a way to tell when we're in our logic tests target, so add something like LOGIC_TESTS to your logic tests target's <code>Preprocessor Macros</code> build setting.</li> <li><p>There's only 1 place in my code where I need to redefine <code>NSLocalizedString</code>, so I was able to place the following code in the header corresponding to that class. If you're having this problem in multiple places, I'd suggest putting the following code in a header and <code>#include</code>-ing it where you need it (I tried using a .pch file but it doesn't work in Logic Tests). Anyway, place this somewhere in the header of class(es) that use <code>NSLocalizedString</code>:</p> <pre><code>#ifdef LOGIC_TESTS #undef NSLocalizedString #define NSLocalizedString(key, comment) [[NSBundle bundleWithIdentifier:@"YOUR_IDENTIFIER"] localizedStringForKey:(key) value:@"" table:nil] #endif </code></pre></li> </ol> <p>Replace <code>YOUR_IDENTIFIER</code> with the Bundle Identifier of your app's bundle (found in your <code>Info.plist</code> file, key is <code>CFBundleIdentifier</code>). This assumes that you've defined <code>LOGIC_TESTS</code> as a preprocessor macro <em>only</em> in your Logic Tests target.</p> <p><strong>edit:</strong> Curiously, once I removed some debugging code this solution stopped working. It looks like you have to trick Xcode into loading the bundle as well. The following does it:</p> <pre><code>NSString *path = @"path_to_main_bundle"; NSBundle *bundle = [NSBundle bundleWithPath:path]; NSLog(@"bundles: %@", [NSBundle allBundles]); </code></pre> <p>Where <code>path_to_main_bundle</code> is <code>== [[NSBundle mainBundle] bundlePath]</code> when you run your main target. Just log it once in gdb or using <code>NSLog</code> on your app delegate to grab the path. It should look something like <code>/Users/YOUR_USER_NAME/Library/Application Support/iPhone Simulator/4.1/Applications/UUID_LOTS_OF_LETTERS_AND_NUMBERS_HERE/App.app</code>.</p> <p>I placed that code in the setUp call for one of my logic test classes. And no, I have no idea why I have to log allBundles to make it work, so anyone who has a clue, please let me know!</p>
 

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