ios - 从 objective-c 调用 swift 函数会出现链接器错误

标签 ios swift swift3

我正在尝试从 objective-c 文件调用 swift 函数。

Swift 函数实现:

@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;
}

构建时链接器错误:

 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)

此代码在 Xcode 7.3 中运行良好,但升级到 8.3 后,它开始抛出链接器错误。

请帮忙。提前致谢。

最佳答案

导入名为 Project name 的文件,并在 Objective-C 文件中添加 -Swift.h

请检查下面的导入文件示例

#import “ProjectName-Swift.h”

当您使用 .swift 文件构建项目时,Xcode 将生成此文件。

此外,您可以通过以下问题进行检查: How to import Swift code to Objective-C

关于ios - 从 objective-c 调用 swift 函数会出现链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43414499/

相关文章:

ios - UILabel 背景颜色泄漏到边框

ios - 如何使用 CNContacts 快速获取手机号码?

swift - 从 '[NSObject : AnyObject]?' 转换为无关类型 'NSDictionary' 总是失败

ios - 使用选项卡 Controller 时出现奇怪的滚动行为 - iOS

ios - UIViewControllerAnimatedTransitioning 与 MPMoviePlayerViewController

ios - 在 Swift 中使用 AVAudioEngine 点击麦克风输入

generics - 迅速 : How to write generic function that calls specialized ones

ios - 生成 DataMatrix 二维码 (ECC 200)

swift - 如何将变量的值设置为在日期选择器中选择的小时值

ios - AFNetworking with AFHTTPClient with AFJSONRequestOperation//MIME-Type Issues