我尝试在我的应用程序中集成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
    }
}

当前回答

我的问题是-swift.h文件的自动生成不能理解CustomDebugStringConvertible的子类。我把class改为NSObject的子类。在此之后,-swift.h文件现在正确地包含了类。

其他回答

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

Step by Step Swift integration for Xcode object -based project:

Create new *.swift file (in Xcode) or add it by using Finder. Create an Objective-C bridging header when Xcode asks you about that. Implement your Swift class: import Foundation // use @objc or @objcMembers annotation if necessary class Foo { //.. } 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 Import Swift interface header in your *.m file. #import "myproject-Swift.h" Don't pay attention to errors and warnings. Clean and rebuild your Xcode project. Profit!

我有同样的问题,原来模块名称中的特殊符号被xcode取代(在我的情况下,破折号最终成为下划线)。在项目设置中检查“模块名称”以查找项目的模块名称。之后使用ModuleName-Swift.h或在设置中重命名模块。

我遇到的问题是,我会在objective-c桥接头中添加类,而在那些被导入的objective-c头中,它们试图导入swift头。它不喜欢那样。

因此,在我所有使用swift但也桥接的objective-c类中,关键是确保在头文件中使用前向类声明,然后在.m文件中导入“*-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"

好吧,在阅读了所有的评论和尝试、阅读、再尝试之后,我设法将swift类包含到我的Big obj-c项目中。 所以,谢谢你的帮助。 我想分享一个技巧,帮助我更好地理解这个过程。 在.m类中,转到swift目标名称的导入行#import "myTargetName-Swift.h" 然后点击键:

命令+鼠标点击->跳转到定义

在那里你可以看到从swift到obj-c的所有转换,在那里你会发现在obj-c中重新声明的各种函数。 希望这条建议能像帮助我一样帮助你。