1

I have an iOS project that uses both Obj-C and Swift.

In main.m, I want to instantiate a variable to be accessed by Swift. The specific use case is to store the absolute time at main, and then measure time elapsed since main elsewhere.

Here's what my relevant files look like:

main.m

#import <UIKit/UIKit.h>

CFAbsoluteTime StartTime;

int main(int argc, char* argv[])
{

    StartTime = CFAbsoluteTimeGetCurrent(); // This is what I want to access elsewhere.
    @autoreleasepool {
        ...
    }
}

myapp-Bridging-Header.h

#import "AppDelegate.h"
extern CFAbsoluteTime StartTime;

AppDelegate.h

#import <UIKit/UIKit.h>

extern CFAbsoluteTime StartTime;

@interface AppDelegate : NSObject <UIApplicationDelegate>{}

@property (nonatomic, strong) IBOutlet UIWindow* window;
// ... other irrelevant @property

@end

And then in the actual .swift file, I'm just trying to access StartTime without declaring anything (as I thought it worked since it's being externed), but I'm getting an unresolved identifier error.

leonsas
  • 4,718
  • 6
  • 43
  • 70
  • FYI, if you have `extern` declaration in your `.h` file that you're importing in your bridging header, you don't need to repeat `extern` declaration again in the bridging header file, itself. Bottom line, the above works fine for me. I wonder if your bridging header is being read at all. You might want to double check the build settings where bridging header is set. Can you see `AppDelegate` class from Swift, too? That's an easy way to check that the bridging header is being picked up properly... – Rob Jan 26 '17 at 02:29
  • Ah, for some reason there were 2 bridging headers, and the one shown in xcode was not the one in build settings. That fixed it. – leonsas Jan 26 '17 at 02:58

0 Answers0