Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode 8 Ambiguous expansion of macro NSLocalizedString

I have the following in my unit test .pch file to allow my unit tests to find the right bundle for the localization files and this was working fine until I upgraded to Xcode8.

#undef NSLocalizedString
#define NSLocalizedString(key, comment) [[NSBundle bundleWithIdentifier:@"Tests-Unit"] localizedStringForKey:(key) value:@"" table:nil]

Now, I get the warning

Ambiguous expansion of macro NSLocalizedString 

Which would explain why my unit tests fail as they can't find the localized string value anymore.

Any ideas what is wrong there? Is there something I need to now do differently?

EDIT: I have a feeling it has something to do with all my source file being in 2 targets i.e one for the project and one for the unit tests which is the way we had it setup. I'm trying to clean this up and removed all my source file from the unit test target and added the

@testable import ProjectName

instead to a Unit test file to be able to access my code for testing but i'm now getting

File 'MyFile.swift' is part of module 'ProjectName'; ignoring import.
like image 647
m.y Avatar asked Sep 22 '16 09:09

m.y


2 Answers

I eventually got it all working again.

Let’s pretend our project is called Panda and it consists of both Obj-C and Swift files. All of those files are in both our Panda and PandaTests target.

STEP 1: Ensure all your files have the right Target Membership i.e. Panda target: Only the source files, development frameworks, images etc and PandaTests target: Only the test files, testing frameworks, mock data etc

STEP 2: Ensure your Panda Project has the Build Settings -> Enable Testability set to Yes.

STEP 3: Ensure your PandaTests Project has the Build Settings -> Product Module Name set to PandaTests.

STEP 4: Do a nice clean by holding down the Option button and then clicking on Product. You should see a Clean Build Folder option.

STEP 5: For Swift Unit Tests, add @testable import Panda. Since we have now removed all our source code files from the PandaTests target, the unit tests need a way of accessing our project files. This enables our Swift unit tests to access all our Swift files and those Obj-C files that have been included in our Panda-Bridging-Header.h.

like image 137
m.y Avatar answered Nov 20 '22 12:11

m.y


In your project Build Settings add -Wno-ambiguous-macro to Other C Flags

It solves the issue (warning is gone), but not sure what are the side effects.

like image 22
JSK Avatar answered Nov 20 '22 13:11

JSK