如何在 Objective C 项目中使用带有 Objective 代码的 Swift 包 (SPM)?

Pha*_*m59 1 objective-c package swift

我看到过关于能够通过 SPM 使用混合语言项目的褒贬不一的评论。我有一个包含 Objective-C 代码的框架。我在混合语言项目(Objective-C 和 Swift)中使用该框架;具体来说,我需要在 Objective-C 代码中使用它。我已将框架转换为 Swift 包。该包编译良好。但我无法让 Objective-C 代码识别我的 Swift 包。旧框架一切正常。我已将新的 Swift 包添加到项目中。

当我使用“@import CalculatorModel;”时 我收到“未找到模块‘CalculatorModel’”。

我正在尝试将 header 导入到 Objective-C 代码中,如下所示:

#import <CalculatorModel/CalculatorModel.h>
Run Code Online (Sandbox Code Playgroud)

但是,当然,找不到标头。

这是我的 package.swift。

let package = Package(
    name: "CalculatorModel",
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "CalculatorModel",
            targets: ["CalculatorModel"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages this package depends on.
        .target(
            name: "CalculatorModel",
            dependencies: []),
        .testTarget(
            name: "CalculatorModelTests",
            dependencies: ["CalculatorModel"]),
    ]
)
Run Code Online (Sandbox Code Playgroud)

如何在包含 Objective-C 的项目中使用包含 Objective C 的 Swift 包?

小智 5

    \n
  1. 确保您的头文件 ( CalculatorModel.h) 是公开的:
  2. \n
\n
    \n
  • 在文件中使用publicHeadersPath& :headerSearchPathPackage.swift
  • \n
\n
// Package.swift\n\nlet package = Package(\n    name: "CalculatorModel",\n    products: [\n        // Products define the executables and libraries a package produces, and make them visible to other packages.\n        .library(\n            name: "CalculatorModel",\n            targets: ["CalculatorModel"]),\n    ],\n    dependencies: [\n        // Dependencies declare other packages that this package depends on.\n        // .package(url: /* package url */, from: "1.0.0"),\n    ],\n    targets: [\n        // Targets are the basic building blocks of a package. A target can define a module or a test suite.\n        // Targets can depend on other targets in this package, and on products in packages this package depends on.\n        .target(\n            name: "CalculatorModel",\n            publicHeadersPath: "include",\n            cSettings: [\n                .headerSearchPath("."),\n            ]\n        ),\n        .testTarget(\n            name: "CalculatorModelTests",\n            dependencies: ["CalculatorModel"]),\n    ]\n)\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  • 将头文件添加到正确的子目录:
  • \n
\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 README.md\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Package.swift\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 Sources\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 CalculatorModel\n    \xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 include\n    \xe2\x94\x82       \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 CalculatorModel.h\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  1. @import尝试在头文件中使用:
  2. \n
\n
// Package.swift\n\nlet package = Package(\n    name: "CalculatorModel",\n    products: [\n        // Products define the executables and libraries a package produces, and make them visible to other packages.\n        .library(\n            name: "CalculatorModel",\n            targets: ["CalculatorModel"]),\n    ],\n    dependencies: [\n        // Dependencies declare other packages that this package depends on.\n        // .package(url: /* package url */, from: "1.0.0"),\n    ],\n    targets: [\n        // Targets are the basic building blocks of a package. A target can define a module or a test suite.\n        // Targets can depend on other targets in this package, and on products in packages this package depends on.\n        .target(\n            name: "CalculatorModel",\n            publicHeadersPath: "include",\n            cSettings: [\n                .headerSearchPath("."),\n            ]\n        ),\n        .testTarget(\n            name: "CalculatorModelTests",\n            dependencies: ["CalculatorModel"]),\n    ]\n)\n
Run Code Online (Sandbox Code Playgroud)\n