Greg Lukosek Greg Lukosek - 3 years ago 132
Objective-C Question

Calling Objective-C from Swift class causing linker errors

I'm trying to use RFDuino Objective-C library in Swift. It all sounds simple, I have called Objective-C from Swift classes before, no problem. This time however I hit the brick wall.

Created header file. Added header files to it. Swift can see the classes no problem. Project compiles fine.

The problem during build appears just after I try to call any of Objective classes

for example:

override func viewDidLoad() {

let rfDuinoManager: RFduinoManager = RFduinoManager.sharedRFduinoManager()


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

enter image description here

Im not sure what Im missing here.
Link to the project here.

Thanks in advance for help. Honestly loosing hope now and considering rewrite whole library to Swift

Rob Rob
Answer Source

The problem is how the rfduino folder was added to the project. So

  1. Remove rfduino folder (selecting rfduino folder in the project navigator panel on left and hit delete button, but when it asks to move it to the trash or just remove references, choose "remove references".

    enter image description here

  2. Your bridging header has an absolute path reference, I'd suggest removing that altogether by selecting it in build settings and hitting delete button:

    enter image description here

  3. Re-add rfduino files back to the project, this time, under "added folders" option, choose "create groups" rather than "create folder references". If you want it to prompt to create proper bridging header for you, don't select folder, but select the individual files:

    enter image description here

    Also make sure that SmartHomeHub is checked below.

  4. If you do that properly it will ask you to create bridging header automatically:

    enter image description here

  5. Go to this new bridging header and add your import lines again:

    enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download