我尝试在我的应用程序中集成Swift代码。我的应用程序是用Objective-C编写的,我添加了一个Swift类。我做了这里描述的所有事情。但我的问题是Xcode还没有创建-Swift.h文件,只有桥接头。我创建了它,但它实际上是空的。 我可以在Swift中使用我所有的ObjC类,但我不能反之亦然。我用@objc标记了我的swift类,但它没有帮助。我现在该怎么办?

编辑:苹果说:“当你将Swift代码导入Objective-C时,你依靠xcode生成的头文件将这些文件公开给Objective-C。[…此头文件的名称是您的产品模块名称,后跟“-Swift.h”。

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

    //MainMenu.m

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

    @implementation MainMenu

这是我的FBManager.swift文件:

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

当前回答

在Objective-C中使用Swift类

如果你要在App Target中导入代码(在一个项目中混合Objective-C和Swift),你应该使用下一个导入行#import "<#YourProjectName#>-Swift.h"将Swift代码暴露给Objective-C代码[在一个项目中混合Swift和Objective-C代码]

在这篇文章中,我将描述如何将Swift静态库导入Objective-C代码

Objective-C consumer -> Swift静态库

Xcode 10.2.1版

创建Swift静态库

跟随创建Swift静态库与下一步添加:

公开Swift API。从Objective-C中使用Swift函数[关于]

在构建之后,你应该找到一个<product_name>-Swift.h文件,该文件应该位于DerivedSources [file not found]

Objective-C消费者与Swift静态库

拖拽二进制文件到Xcode项目[关于]

链接库[未定义的符号][链接vs嵌入]

Project editor -> select a target -> General -> Linked Frameworks and Libraries -> add -> Add Others... -> point to `lib<product_name>.a` file
//or
Project editor -> select a target -> Build Phases -> Link Binary With Libraries -> add -> Add Others... -> point to `lib<product_name>.a` file

添加库搜索路径[未找到库][递归路径]

Project editor -> select a target -> Build Settings -> Search Paths -> Library Search paths -> add path to the parent of `lib<product_name>.a` file

添加头搜索路径[模块未找到][递归路径]

Project editor -> select a target -> Build Settings -> Search Paths -> Header Search Paths -> add path to generated `<product_name>-Swift.h` file 

向Objective-C项目添加空的.swift文件。当Xcode询问时,按下Create Bridging Header(它将创建module_name-Bridging-Header.h),并为该文件设置一个路径

Project editor -> select a target -> Build Settings -> Swift Compiler - General -> Objective-C Bridging Header

导入模块到Objective-C客户端代码[文件未找到][module_name]

#import "module_name-Swift.h"

这里有更多的例子

其他回答

存档将成功进行测试

如何在objective - c项目或react本地项目中调用swift函数(也可以在swift框架中)

它也适用于react-native 遵循以下步骤:

打开生成设置并检查这些参数: 定义模块:YES

在AppDelegate.h

#import <React/RCTBridgeDelegate.h>
#import <UIKit/UIKit.h>

@class KB;

@interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate>

@property (strong, nonatomic) KB *appkb;

@end

在AppDelegate.m

#import "AppDelegate.h"
#import "ProductModuleName-Swift.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  [self.appkb methodReceiveSwiftClass];
  [self.appkb methodEvents:event prop:properties];
}

@end

KB.swift

import Foundation
// import framework if using a swift framework in objective c or react native native modules.
@objc public class KB:NSObject{
  
  @objc public func methodReceiveSwiftClass(){
    //write anything here....
  }

  @objc public func methodEvents(_ event: String, prop: String){
    //write anything here
     //func with params
  }

}

ProjectModuleName-Bridging-Header.h

#import "React/RCTBridgeModule.h"
#import "AppDelegate.h"

我也遇到了同样的问题,最后我发现他们的目标不同。 ObjC类附加到Target1和Target2, Swift类只附加到Target1,在ObjC类中不可见。

希望这能帮助到一些人。

只包括 以。m或。h格式导入“myProject-Swift.h”文件

附注:你不会在文件检查器中找到“myProject-Swift.h”,它是隐藏的。但它是由应用程序自动生成的。

对于Swift 5:

将@objc关键字添加到类和方法中 向类和方法添加public关键字 让你的类继承自NSObject 建设项目 在Objective-C文件中输入“MyProject-Swift.h” @objc MyClass: NSObject { @objc myMethod() { } }

我的问题是我在xcode创建桥文件后卡住了,但我仍然在头文件MYPROJECTNAME-swift.h中得到了错误

1.我检查终端和搜索所有自动创建的swift桥文件:

find ~/library/Developer/Xcode/DerivedData/ name "*- swift .h"|xargs basename|sort - . find ~/library/Developer/Xcode/DerivedData/ name "*- swift .h

你看xcode创建了什么。

在我的例子中,我在我的项目名称中有空格,xcode将其替换为_