3

环境

  • macOS:10.15.3
  • Xcode:11.3.1
    • 斯威夫特:5.1
    • 应用目标:macOS

背景

我有一个基于 NSView 的 IBDesignable 控件(或 UIView 取决于所需的目标)。我使用包管理器将其打包如下:

// 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 都希望以 AppleTV 作为目标来呈现这个包,从而呈现我的控制权。鉴于我上面使用的类型别名,控件无法正确呈现,因为(显然)macOS 对 UIColor、UIFont、UIView、UIViewController 一无所知。

问题

有没有办法在渲染控件时强制 IB 使用特定的目的地?如果没有,我的项目设置中是否缺少一些东西?就像我说的,控制按预期工作;我只是希望它在 IB 中呈现。

4

1 回答 1

1

`因为提供的材料不足以测试问题,只是建议 - 尝试以下条件映射

#if canImport(AppKit)
    import AppKit
    public typealias QJViewController = NSViewController
    public typealias QJColor = NSColor
    public typealias QJFont = NSFont
    public typealias QJView = NSView
#elseif canImport(UIKit)
    import UIKit
    public typealias QJViewController = UIViewController
    public typealias QJColor = UIColor
    public typealias QJFont = UIFont
    public typealias QJView = UIView
#endif
于 2020-03-06T17:25:05.957 回答