Can I use Swift Package Manager in Obj-C code?

  1. Create a directory Framework/include and symlink or copy these headers into it:

    Capture d’écran 2020-04-17 à 16 h 35 min 34 s

    This is only necessary because the headers import each other in such a way that they assume they will have all been copied into the same directory before they are actually used.

  2. Add this Package.swift:

// swift-tools-version:5.2

import PackageDescription

let package = Package(
  name: "MASShortcut",
  products: [
    .library(name: "MASShortcut", targets: ["MASShortcut"])
  ],
  targets: [
    .target(
      name: "MASShortcut",
      path: "Framework",
      exclude: [
        "Model/MASShortcutTests.m",
        "Monitoring/MASHotKeyTests.m",
        "Monitoring/MASShortcutMonitorTests.m",
        "User Defaults Storage/MASDictionaryTransformerTests.m",
        "User Defaults Storage/MASShortcutBinderTests.m"
      ],
      cSettings: [
        .headerSearchPath("UI"),
      ]
    )
  ]
)
  1. Add #import <AppKit/AppKit.h> to the top of any file that still does not compile. (Xcode was doing this automatically with the .pch file).

These instructions are enough to build and import. (They do not set up any test target or do anything with the resources.)

5 Likes