在Objective-C应用程序中集成Swift项目

时间:2017-05-17 10:08:59

标签: ios objective-c swift

我正在尝试在一个Objective-C应用程序中集成Swift项目,我添加了标题“project-Swift.h”,我在Swift类之前添加了@objc, 我解决了兼容性问题,但这些也没有为他们找到解决方案:

 func postCallsChangedNotification() {
        NSNotificationCenter.Default.post(name: type(of: self).CallsChangedNotification, object: self)
    }

错误:使用未解析的标识符'type'

func startSpeakerboxCall(completion: ((_success: Bool) -> Void)?) {
    // Simulate the call starting successfully
    completion?(_success: true)

    /*
        Simulate the "started connecting" and "connected" states using artificial delays, since
        the example app is not backed by a real network service
     */
    DispatchQueue.main.asyncAfter(wallDeadline: DispatchWallTime.now() + 3) {
        self.hasStartedConnecting = true

        DispatchQueue.main.asyncAfter(wallDeadline: DispatchWallTime.now() + 1.5) {
            self.hasConnected = true
        }
    }
}

错误:使用未解析的标识符'DispatchQueue'/使用未解析的标识符'DispatchWallTime'

任何人都可以帮助找出这些错误出现的原因但是这段代码在集成之前是否正常工作?

2 个答案:

答案 0 :(得分:2)

看起来您正在使用使用旧版Swift语言版本>是的,所以DispatchQueue不可用,你正在编译Swift 2.3而不是3,你有两个选择:

  • 使用swift 3。
  • 将您的功能更新为2.3语法。

答案 1 :(得分:0)

当然,您可以在Objective c项目中使用swift文件。

确保在目标c文件中导入了正确的ProjectName.swift.h文件。

#import "ProjectName-Swift.h"

Refer swift usage in objective c project