Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extern NSString not accessible since Swift 2.2

Tags:

xcode

swift

Since the update to Xcode 7.3 with Swift 2.2 I am not able to access variables from an external Objective-C Library.

Since today I was able to access this variables:

extern NSString* const DEFAULT_URL;  

This is defined in an Objective-C Header file from a precompiled .a framework.

In my swift code I only had to call DEFAULT_URL.

Since Swift 2.2 I get the following errror:

Use of unresolved identifier 'DEFAULT_URL'  

I am able to access the classes and methods of this framework, but I can't access extern NSStrings.

Any ideas how to fix this?

like image 405
patrickS Avatar asked Mar 22 '16 07:03

patrickS


2 Answers

@patrickS I had this for a silly reason, my extern const was defined inside an @interface in my .h file. This seems to have made it private to Swift code with this version of XCode / Clang. It applies to all extern consts not just NSString *.

e.g.

//In Foo.h
extern const int kBlah

@interface Foo
...
@end

instead of

//In Foo.h
@interface Foo
extern const int kBlah
...
@end
like image 80
Mark Avatar answered Nov 17 '22 15:11

Mark


I had the same problem and as in the question, the ext strings were in my case in a dependency-managed (cough pod cough) third party library. So I could not easily move them around without messing everything up in the long run.

I found two solutions:

  1. copy-paste the ext declaration to (the bottom of) your bridging header
  2. write your own static helper class in objective c that provides the ext strings as class methods (and make this static helper available to Swift)

I leave it up to you which of the two solutions you deem less hacky (I went with solution 1, as I am lazy).

like image 36
Thomas Köhn Avatar answered Nov 17 '22 17:11

Thomas Köhn