混合语言框架 [英] Mixed language framework

查看:70
本文介绍了混合语言框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用Objective-C编写的框架(我们称之为MyKit),并在其中添加了一些Swift类.我正在尝试使用以下文档来解决这个问题:

也许我误解了伞头?我在哪里可以说这是伞头文件"?如果将伞形头文件设置为桥接头,则框架会编译,但这听起来像是我已经回到这种开头了吗?

欢呼

尼克

解决方案

我相信您的问题可能出在Swift类中的访问修饰符上,但是我编写了一个简短的指南和示例项目来帮助您:

可以在此处

找到示例项目

拥有混合语言框架有两个部分:

  1. 将Objective-C导入Swift
  2. 将Swift导入Objective-C


1.将Objective-C导入Swift

例如,如果您有一个名为Player的Objective-C类,并且想要添加到名为Game的快捷类中.

根据文档,您需要执行以下两个步骤才能将Player对象导入到Game对象中.

  1. 在打包设置"下的构建设置"下,确保将该框架目标的定义模块"设置设置为是".

  1. 在您的伞形头文件中,导入要公开给Swift的每个Objective-C头.

    #import <Framework/Player.h>
    

确保在Objective-C中的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

要将Swift Game类导入到Player对象中,我们可以遵循类似的过程.

  1. 和以前一样;在打包设置"下的构建设置"下,确保将该框架目标的定义模块"设置设置为是".

  1. 使用以下语法并将相应的名称替换为该框架目标中的Swift代码到该框架目标中的任何Objective-C .m文件中:

    #import <ProductName/ProductModuleName-Swift.h>
    

    就我而言,它的工作方式是:

    #import <SwiftObjC/SwiftObjC-Swift.h>
    

    我想为你:

    #import <MyKit/MyKit-Swift.h>
    

    因此,请确保要访问的所有属性,方法和类在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:

#import <MyKit/ModelObjectA.h>
#import <MyKit/ModelObjectB.h>
#import <MyKit/ControllerObjectC.h>

I don't list ControllerObjectD.swift, even though it goes into here as well? Or should I include

#import <MyKit/ControllerObjectD-Swift.h>

?

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:

  1. Importing Objective-C into Swift
  2. Importing Swift into Objective-C


1. Importing Objective-C into Swift

This is, for example, if you have an Objective-C class named Player that you want to add to a swift class called Game.

According to the documentation, you need to do these two steps to import the Player object into the Game object.

  1. Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to Yes.

  1. In your umbrella header file, import every Objective-C header you want to expose to Swift.

    #import <Framework/Player.h>
    

Make sure your Player header file in Objective-C is marked for public target membership in the framework:

Following these steps, it's possible to import the 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

For importing the Swift Game class into the Player object, we can follow a similar procedure.

  1. As before; Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to Yes.

  1. 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:

    #import <ProductName/ProductModuleName-Swift.h>
    

    In my case, this works as:

    #import <SwiftObjC/SwiftObjC-Swift.h>
    

    and I assume, for you:

    #import <MyKit/MyKit-Swift.h>
    

    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秒登录
发送“验证码”获取 | 15天全站免登陆