1

I'm still in the process of learning Swift, but I have a pretty good understanding of the basics and how it interacts with Objective-C. On my team, we have an Objective-C app and we're experimenting with some Swift in the app. Our standard in the past has been to put our constants in a header file like:

static NSString *const kSomeConstant = @"ConstantValue";

But when I reference this value in Swift, I get a linker error:

Undefined symbols for architecture x86_64:
"_kSomeConstant", referenced from: __TFFC22Commons_Tests36DCTestBTBusinessService25testSuccessfulFetchOffersFS0_FT_T_u0_KT_PSs11BooleanType_ in DCTestBTBusinessService.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

If I change the static const to a #define it compiles successfully but this is not ideal if not necessary. Any idea why this is not linking? Thanks for any help!

Edit: Here is an example of where we are trying to use the value. The CacheManager class is an Objective-C class and containsObjectForKey accepts an NSString as a parameter.

CacheManager.instance().containsObjectForKey(kSomeConstant)

justbaum30
  • 505
  • 2
  • 8
  • 16
  • Sorry, we're using the stable version of XCode currently (6.1.1) so I think that would be Swift 1.1? – justbaum30 Feb 25 '15 at 05:13
  • Updated with an example of using the value in Swift. – justbaum30 Feb 25 '15 at 05:17
  • kSomeConstant is a static NSString *const. This isn't a compile error though this is a linking error so I'm not sure how relevant the Swift code actually is – justbaum30 Feb 25 '15 at 05:20
  • Well whenever we reference a static NSString * const in a Swift file in our project it's giving us a linking error. I'm hoping there is a setting in the project we can set. – justbaum30 Feb 25 '15 at 05:25
  • 1
    I think your problem might be that you're using `static`. In C that means that the variable is local to the file it's defined in. Since you define in it a .h file, that makes it local to every .m/.c file that includes it. Which ends up meaning: there's no actual `kSomeConstant` symbol, but rather a private mangled symbol for each include made. – Juan Feb 25 '15 at 05:25

0 Answers0