简体   繁体   中英

Calling swift function from objective c gives linker error

I am trying to call swift function from objective c file.

Swift function implementation:

@objc class FXFormVariables : NSObject {
    class func FXFontName() -> String { return fontName }
    class func FXFontSize() -> CGFloat { return fontSizeLarge }
    class func FXHiddenCell() -> NSArray { return hiddenElementFromFormIndex as NSArray }
}

Objective C:

NSArray *hideArray = [FXFormVariables FXHiddenCell];
if ([hideArray containsObject:@(cellIndexPath)]){
        return 0.0;
}

Linker error in Buildtime:

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

This code works fine in Xcode 7.3 but after upgrade to 8.3 it start throwing linker error.

Please help. Thanks in advance.

Import file named Project name appending by -Swift.h in your Objective-C file

Please check below example of import file

#import “ProjectName-Swift.h”

Xcode will generate this file when you build your project with .swift files.

Also, you can check from below question: How to import Swift code to Objective-C

you can get this type of error check your class is listed under the "Compile Sources" step of the "Build Phases" tab of your target. Normally Xcode does this for you, but sometimes it loses the plot and you need to add the class file manually.

To do this:

TargetSettings -> Build Phases -> Compile Sources -> add your class -.swift >Build and Run

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM