3

環境

  • macOS: 10.15.3
  • Xcode: 11.3.1
    • スイフト: 5.1
    • アプリケーションの対象: macOS

バックグラウンド

NSView (または目的のターゲットに応じて UIView) に基づく IBDesignable コントロールがあります。次のようにパッケージマネージャーを使用してパッケージ化しました。

// swift-tools-version:5.1
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "MyControl",
    platforms: [
        .macOS(.v10_13),
        .iOS(.v10)
    ],
    products: [
        // Products define the executables and libraries produced by a package, and make them visible to other packages.
        .library(
            name: "MyControl",
            targets: ["MyControl"]),
    ],
    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 which this package depends on.
        .target(
            name: "MyControl",
            dependencies: []),
        .testTarget(
            name: "MyControl Tests",
            dependencies: ["MyControl"]),
    ]
)

「MyControl」の一部として、目的のターゲットターゲットに基づいて次のタイプエイリアスがあります

#if os(macOS)
    import AppKit
    public typealias QJViewController = NSViewController
    public typealias QJColor = NSColor
    public typealias QJFont = NSFont
    public typealias QJView = NSView
#elseif os(iOS) || os(tvOS)
    import UIKit
    public typealias QJViewController = UIViewController
    public typealias QJColor = UIColor
    public typealias QJFont = UIFont
    public typealias QJView = UIView
#endif

パッケージは正常にコンパイルされます。コントロールは目的のプロジェクトに正しくインポートされます。すべてが目的のプロジェクトで期待どおりにコンパイルされ、機能します。

問題

InterfaceBuilder でコントロールをセットアップすると、次のエラーでレンダリングに失敗します。

ここに画像の説明を入力

繰り返しますが、私のターゲットはAppleTV ではなく、macOS です。私が何をしようと、IB はこのパッケージをレンダリングしたいと考えています。上記で使用した typealiases を考えると、(明らかに) macOS は UIColor、UIFont、UIView、UIViewController について何も知らないため、コントロールは正しくレンダリングできません。

質問

コントロールをレンダリングするときに IB に特定の宛先を使用させる方法はありますか? そうでない場合、プロジェクトのセットアップに欠けているものはありますか? 前述のとおり、コントロールは期待どおりに機能します。IBでレンダリングしたいだけです。

4

1 に答える 1