如何在带有SwiftPM的objective-c模块中使用swift模块?

时间:2016-12-13 13:55:50

标签: objective-c swift llvm swift-package-manager

以下是根目录

的示例
├── Package.swift
└── Sources
    ├── Objc-cli
    │   └── main.m
    └── Swifty
        └── MyStruct.swift

Swifty模块只是一个简单的结构

// MyStruct.swift

public struct MyStruct {
    public var text = "Hello, World!"

    public init() {
    }
}

Objc-cli我试着像这样链接swift模块。

// main.m

#import <Foundation/Foundation.h>
@import Swifty <---- Not found

int main() {
    NSLog(@"Hello from Objc");
    return 0;
}

以下是Package.swift的样子:

// Package.swift

import PackageDescription

let package = Package(
    name: "MyTest",
    targets: [
        Target(name: "Swifty", dependencies: []),
        Target(name: "Objc-cli", dependencies: ["Swifty"]),
    ]
)

遗憾的是,编译器无法识别objective-c模块中的Swifty模块。这是输出:

$ swift build
Compile Swift Module 'Swifty' (1 sources)
Compile Objc-cli main.m
/tmp/TestPackage/Sources/Objc-cli/main.m:10:9: fatal error: module 'Swifty' not found
@import Swifty
 ~~~~~~~^~~~~~
1 error generated.
<unknown>:0: error: build had 1 command failures

我错过了什么或现在根本不可能吗?

1 个答案:

答案 0 :(得分:0)

现在无法在C模块中导入Swift模块,它只能以另一种方式工作,将C导入Swift。

您可以在SwiftPM找到详细信息 - C language targets