不能在 Objective-C 中使用 Swift 类 [英] Can't use Swift classes inside Objective-C

查看:28
本文介绍了不能在 Objective-C 中使用 Swift 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在我的应用程序中集成 Swift 代码.我的应用程序是用 Objective-C 编写的,并且我添加了一个 Swift 类.我已经完成了此处.但我的问题是 Xcode 没有创建 -Swift.h 文件,只有桥接头.所以我创建了它,但它实际上是空的.我可以在 Swift 中使用我所有的 ObjC 类,但反之则不行.我用 @objc 标记了我的 swift 类,但它没有帮助.我现在能做什么?

Apple 说:当您将 Swift 代码导入 Objective-C 时,您依赖 Xcode-generated 头文件将这些文件暴露给 Objective-C.[...]此标题的名称是您的产品模块名称,后跟添加-Swift.h".

现在当我想导入该文件时,它给出了一个错误:

//MainMenu.m#import "myProjectModule-Swift.h"//错误:找不到'myProjectModule-Swift.h'文件@实现主菜单

这是我的 FBManager.swift 文件:

@objc 类 FBManager: NSObject {var descr = "FBManager 类"在里面() {超级初始化()}功能描述(){打印(描述)}func getSharedGameState() ->游戏状态{return GameState.sharedGameState()//好的!GameState 是用 Objective-C 编写的,这里没有错误}}

解决方案

我花了大约 4 个小时试图在我的基于 Xcode Objective-C 的项目中启用 Swift.我的 myproject-Swift.h 文件已成功创建,但我的 Xcode 没有看到我的 Swift-classes.所以,我决定创建一个新的 Xcode 基于 Objc 的项目,最后,我找到了正确的答案!希望这篇文章能帮助别人:-)

基于 Xcode Objc 的项目的 Swift 分步集成:

  1. 创建新的 *.swift 文件(在 Xcode 中)或使用 Finder 添加它.
  2. 当 Xcode 询问您时,创建一个 Objective-C 桥接头.
  3. 实现你的 Swift 类:

    导入基础//必要时使用@objc 或@objcMembers 注释类 Foo {//..}

  4. 打开构建设置并检查这些参数:

    • 定义模块: YES<块引用>

      复制&在搜索栏中粘贴参数名称

    • 产品模块名称: myproject<块引用>

      确保您的产品模块名称不包含任何特殊字符

    • 安装 Objective-C 兼容性标题: YES<块引用>

      *.swift 文件添加到项目后,此属性将出现在构建设置中

    • Objective-C 生成的接口头文件: myproject-Swift.h<块引用>

      此标头由 Xcode 自动生成

    • Objective-C 桥接标题: $(SRCROOT)/myproject-Bridging-Header.h
  5. 在 *.m 文件中导入 Swift 接口标头.

    #import "myproject-Swift.h"

    <块引用>

    不要关注错误和警告.

  6. 清理并重建您的 Xcode 项目.
  7. 利润!

I try to integrate Swift code in my app.My app is written in Objective-C and I added a Swift class. I've done everything described here. But my problem is that Xcode haven't created the -Swift.h file, only the bridging headers. So I created it, but it's actually empty. I can use all my ObjC classes in Swift, but I can't do it vice versa. I marked my swift class with @objc but it didn't help. What can I do now?

EDIT: Apple says:" When you import Swift code into Objective-C, you rely on an Xcode-generated header file to expose those files to Objective-C. [...] The name of this header is your product module name followed by adding "-Swift.h". "

Now when I want to import that File, it gives an error:

    //MainMenu.m

    #import "myProjectModule-Swift.h" //Error: 'myProjectModule-Swift.h' file not found

    @implementation MainMenu

Here is my FBManager.swift file:

@objc class FBManager: NSObject {

    var descr = "FBManager class"

    init() {
        super.init()
    }

    func desc(){
        println(descr)
    }

    func getSharedGameState() -> GameState{
        return GameState.sharedGameState() //OK! GameState is written in Objective-C and no error here
    }
}

解决方案

I spent about 4 hours trying to enable Swift in my Xcode Objective-C based project. My myproject-Swift.h file was created successfully, but my Xcode didn't see my Swift-classes. So, I decided to create a new Xcode Objc-based project and finally, I found the right answer! Hope this post will help someone :-)

Step by step Swift integration for Xcode Objc-based project:

  1. Create new *.swift file (in Xcode) or add it by using Finder.
  2. Create an Objective-C bridging header when Xcode asks you about that.
  3. Implement your Swift class:

    import Foundation
    
    // use @objc or @objcMembers annotation if necessary
    class Foo {
        //..
    }
    

  4. Open Build Settings and check these parameters:

    • Defines Module : YES

      Copy & Paste parameter name in a search bar

    • Product Module Name : myproject

      Make sure that your Product Module Name doesn't contain any special characters

    • Install Objective-C Compatibility Header : YES

      Once you've added *.swift file to the project this property will appear in Build Settings

    • Objective-C Generated Interface Header : myproject-Swift.h

      This header is auto-generated by Xcode

    • Objective-C Bridging Header : $(SRCROOT)/myproject-Bridging-Header.h
  5. Import Swift interface header in your *.m file.

    #import "myproject-Swift.h"
    

    Don't pay attention to errors and warnings.

  6. Clean and rebuild your Xcode project.
  7. Profit!

这篇关于不能在 Objective-C 中使用 Swift 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆