3

Environment

  • macOS: 10.15.3
  • Xcode: 11.3.1
    • Swift: 5.1
    • Application Target: macOS

Background

I have an IBDesignable control based on NSView (or UIView depending on the desired target). I have packaged it using Package Manager as follows:

// 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"]),
    ]
)

As part of "MyControl", I have the following typealiases based on the desired destination Target

#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

The Package compiles successfully; the control imports into my destination project correctly; everything compiles and functions as expected in the destination project.

The Problem

When I setup the control in InterfaceBuilder, it fails to render with the following error:

enter image description here

To reiterate: My Target is not appleTV, it is macOS. Regardless of what I do, IB wants to render this package, and hence my control, with AppleTV as the destination. Given the typealiases I use above, the control fails to render correctly because (obviously) macOS knows nothing about UIColor, UIFont, UIView, UIViewController.

Question

Is there a way to force IB to use a particular destination when rendering a control? If not, is there something I am missing from my project setup? Like I said, the control works as expected; I would just like it to render in IB.

Mike Manzo
  • 150
  • 1
  • 7
  • I'm experiencing the same issue... My Package will run only under iOS (specified in the manifest), and the IBDesignable works in a normal project but inside a Swift Package IB tries to render for tvOS... Did you find a solution? – DungeonDev Sep 16 '20 at 15:07
  • I am also experiencing the same issue... I've defined iOS as the only platform available in my Package.swift file but IB is still trying to render it on tvOS. Crazy. – Ayoze Roberto Bernardo Sep 28 '20 at 15:35

1 Answers1

1

`Cause provided materials are not enough to test the issue, just suggestion - try instead the following conditional mapping

#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
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • Thanks, Asperi! This suggestion 'fixes' the failure in IB. However, my issue still remains. My targeted application and destination is macOS; however, IB tries to render the control using "AppleTV 4K" as its destination. Sadly, I still get the following: "Designables Build Failed". – Mike Manzo Mar 08 '20 at 19:14