Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Swift function from Objective-C class

I have an old Objective-C project and I want to call new Swift function and object, I have create the file "<ProjectName>-Bridging-Header.h" and "<ProjectName>-Swift.h"

was easy for me call the function from Swift to Objective-C but I have a problem for reverse.

So I have create a simple class "System.Swift"

import Foundation  @objc class System : NSObject {      @objc func printSome() {         println("Print line System");     }      } 

now I have try to follow the documentation here and inside the <...>-Swift.h file I have write this

@class System;  @interface System : NSObject  -(void)printSome;  @end 

and I have import it inside my Objective-C Class. At this point inside my Objective C class (currently UIViewController) of my Objective-C code I have try to call "printSome" method:

- (void)viewDidLoad {     [super viewDidLoad];     System * sis = [[System alloc] init];     [sis printSome];     //any additional setup after loading the view from its nib. } 

now I have the following Error:

Undefined symbols for architecture i386: "OBJC_CLASS$_System", referenced from: objc-class-ref in "ObjectiveC_Class_That_Call_Swift_Object".o ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1 (use -v to see invocation)

like image 882
Eloreden Avatar asked Jun 06 '14 09:06

Eloreden


People also ask

Can you call Swift function from Objective-C?

Call Swift from Objective-CThe Swift library cannot be directly called from Objective-C, since it is missing the required annotations in the code, and in many cases, modules do not inherit from NSObject, rather they use the native Swift data types.

How do I access a Swift file in Objective-C?

Import Swift code into Objective-C within the same framework: Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to Yes. Import the Swift code from that framework target into any Objective-C .

Can an Objective-C class implement a Swift protocol?

In order to use Swift code inside Objective-C one must scrifice some Swift features and write a wrapper around original Swift code that won't use non-compatible features (like structs, generics, enum associated values, protocol extensions etc.). All wrapper classes must inherit NSObject .


2 Answers

Problem Solved, I previously create and included a new .h file in my Objective-C class named <ProductModuleName>-Swift.h but, as i discovered later, this step is not necessary because the compiler creates the necessary file invisible.

Simply include <ProductModuleName>-Swift.h in your class and it should work.

like image 189
Eloreden Avatar answered Oct 18 '22 19:10

Eloreden


It is strange but will work after we do:

  1. Add @objc to your Swift-class ("MySwiftClass").

  2. Add in Obj-C, i.e. the .m file:

    #import "(ProjectName)-Swift.h" 
  3. Declare in header .h

    @class MySwiftClass; 

Compiler will generate the interface for @objc marked class in MyModuleName-Swift.h file.

Auto-Generated Obj-C Example:

SWIFT_CLASS("_TtC10Project17220PLHelper") @interface PLHelper  + (void)notifyForDownloading:(NSDictionary *)userInfo; - (instancetype)init OBJC_DESIGNATED_INITIALIZER; @end 
like image 41
Svitlana Avatar answered Oct 18 '22 21:10

Svitlana