Search code examples
iosswiftxcode8ios10.3fxforms

Linker error when passing data from Swift to Objective C


I am using FXForm Library and want to get data from my Swift file in Objective C file function.

Demo Project Link

Swift Code Implimnetation:

let fontName  = "HelveticaNeue"
let fontSizeLarge:CGFloat = 14.0
var hiddenElementFromFormIndex = [Int]()

//fx form variables
@objc class FXFormVariables : NSObject {
    public override init() {}
    class func FXFontName() -> String { return fontName }
    class func FXFontSize() -> CGFloat { return fontSizeLarge }
    class func FXHiddenCell() -> NSArray { return hiddenElementFromFormIndex as NSArray }
}

In Objective C file, I am getting error when we write below like:

NSArray *hideArray = [FXFormVariables FXHiddenCell];

I created the bridge header file correctly and Target Membership is checked in the Objective C file.

Error I am getting:

 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)

Architectures and Valid Architectures are:

armv7 and armv64

Solution

  • To use Swift class inside Objective-C file you need to import Xcode-generated header file in your Objective-C file.

    #import "ProductModuleName-Swift.h"
    

    I created a Test project with the same name.

    Swift File: // // FXFormVariables.swift // Test // // Created by Puneet Sharma2 on 12/07/17. // Copyright © 2017 com.puneet.sh2525. All rights reserved. //

    import Foundation
    import UIKit
    
    let fontName  = "HelveticaNeue"
    let fontSizeLarge:CGFloat = 14.0
    var hiddenElementFromFormIndex = [Int]()
    
    //fx form variables
    @objc class FXFormVariables : NSObject {
        public override init() {}
        class func FXFontName() -> String { return fontName }
        class func FXFontSize() -> CGFloat { return fontSizeLarge }
        class func FXHiddenCell() -> NSArray { return hiddenElementFromFormIndex as NSArray }
    }
    

    Objective-C File

    #import "ABC.h"
    #import "Test-Swift.h"
    
    @implementation ABC
    
    
    - (void)drawRect:(CGRect)rect {
        NSArray *hideArray = [FXFormVariables FXHiddenCell];
    }
    

    You can read more about it here.