混合语言框架 [英] Mixed language framework
问题描述
我有一个用Objective-C编写的框架(我们称之为MyKit),并在其中添加了一些Swift类.我正在尝试使用以下文档来解决这个问题:
也许我误解了伞头?我在哪里可以说这是伞头文件"?如果将伞形头文件设置为桥接头,则框架会编译,但这听起来像是我已经回到这种开头了吗? 欢呼 尼克 我相信您的问题可能出在Swift类中的访问修饰符上,但是我编写了一个简短的指南和示例项目来帮助您:>
可以在此处 拥有混合语言框架有两个部分:
例如,如果您有一个名为 根据文档,您需要执行以下两个步骤才能将 在您的伞形头文件中,导入要公开给Swift的每个Objective-C头. 确保在Objective-C中的 按照这些步骤,可以将
要将Swift 使用以下语法并将相应的名称替换为该框架目标中的Swift代码到该框架目标中的任何Objective-C .m文件中: 就我而言,它的工作方式是: 我想为你: 因此,请确保要访问的所有属性,方法和类在swift文件中都定义为public,否则它们将对Objective-C不可见. 我已经上传了示例项目,显示了这一切如何在GitHub上工作 https://github.com/elliott -minns/SwiftObjCTestFramework 我希望这可以帮助您解决问题. I have a framework (let's call it MyKit) written in Objective-C that I'm extending with some Swift classes. I'm trying to get my head around it using this documentation: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_77 As far as I understand, I'm not supposed to have a bridging header class, and instead put all my includes in the umbrella header file (that I understand to be ). So I write in MyKit.h: I don't list ControllerObjectD.swift, even though it goes into here as well? Or should I include ? ControllerObjectD uses ModelObjectA and ModelObjectB. Now that I don't have a bridge headerfile, I get compile errors in it because it cannot find these objects. The documentation says "Swift will see every header you expose publicly in your umbrella header." and this is true when I import the framework into other projects, but the framework project cannot compile because it doesn't see it. I have turned on the "Define Modules" build setting. Is there something I've misunderstood about the umbrella header, perhaps? Where can I say "hi project, this is the umbrella header file"? The framework compiles if I set the umbrella header file as bridging header, but that sounds like I've come back to the beginning this way? Cheers Nik I believe your problem may be down to the Access Modifiers in your Swift class, however I've written a short guide and sample project to help you: Sample project can be found here There are two parts to having a mixed language framework:
This is, for example, if you have an Objective-C class named According to the documentation, you need to do these two steps to import the In your umbrella header file, import every Objective-C header you want to expose to Swift. Make sure your Following these steps, it's possible to import the
For importing the Swift Import the Swift code from that framework target into any Objective-C .m file within that framework target using this syntax and substituting the appropriate names: In my case, this works as: and I assume, for you: Therefore, make sure that all the properties, methods and classes you want to access are defined as public in your swift file or else they won't be visible to Objective-C . I have uploaded my sample project showing how this all works on GitHub https://github.com/elliott-minns/SwiftObjCTestFramework I hope this helps you with your problem. 这篇关于混合语言框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
1.将Objective-C导入Swift
Player
的Objective-C类,并且想要添加到名为Game
的快捷类中.Player
对象导入到Game
对象中.
#import <Framework/Player.h>
Player
头文件中标记了框架中的公共目标成员身份:Player
Objective-C类导入到Game
Swift类中:import UIKit
public class Game: NSObject {
public let player: Player
public init(player: Player) {
self.player = player
super.init();
}
}
2.将Swift导入Objective-C
Game
类导入到Player
对象中,我们可以遵循类似的过程.
#import <ProductName/ProductModuleName-Swift.h>
#import <SwiftObjC/SwiftObjC-Swift.h>
#import <MyKit/MyKit-Swift.h>
#import <MyKit/ModelObjectA.h>
#import <MyKit/ModelObjectB.h>
#import <MyKit/ControllerObjectC.h>
#import <MyKit/ControllerObjectD-Swift.h>
1. Importing Objective-C into Swift
Player
that you want to add to a swift class called Game
.Player
object into the Game
object.
#import <Framework/Player.h>
Player
header file in Objective-C is marked for public target membership in the framework:Player
Objective-C class into the Game
Swift Class:import UIKit
public class Game: NSObject {
public let player: Player
public init(player: Player) {
self.player = player
super.init();
}
}
2. Importing Swift into Objective-C
Game
class into the Player
object, we can follow a similar procedure.
#import <ProductName/ProductModuleName-Swift.h>
#import <SwiftObjC/SwiftObjC-Swift.h>
#import <MyKit/MyKit-Swift.h>