Reputation: 457
I am trying to create Swift Packages using the built-in features of XCode 11, but I am having issues. I am able to create generic Swift Package (no platforms) and Swift Package for Mac (platform .macos), but when I try to create ones for iOS only, the compiler tells me that UIKit can not be imported. I see the message:
"UIKit is not available when building for MacOS. Consider using '#if !os(macOS)' to conditionally import this framework."
I am using XCode to generate this package, not any command line generation of a Swift Package. The package does not have an Xcode project file associated with it, it is simply a package. If I place conditional compiles around the iOS code, the project builds successfully because it skips the iOS portion. However, this way does not show me any compile errors that might be in the code!
If I do compile, and publish with the conditional macros, I can then import the Swift Package into an iOS project, and the code compiles. However, debugging takes forever, since if I have an compile error in the package, I have to edit the Package, commit, push, update, then run it within the Mac project to see if I got it right. I would like to have the Swift Package correct compile the code, so I can fix it there.
Help? Thanks!
Upvotes: 1
Views: 2274
Reputation: 534885
Here are step by step instructions for creating a Swift package for iOS using Xcode 11.5.
Choose File > New > Swift Package.
Accept the default name MyLibrary and save to the desktop.
To the existing MyLibrary.swift file inside Sources/MyLibrary, add an import of UIKit, and just for good measure let's actually refer to a UIKit class. And let's make everything public, so that it looks like this:
import UIKit
public struct MyLibrary {
public init() {}
public var view = UIView()
}
In MyLibraryTests.swift
inside Tests/MyLibraryTests, delete the entire final class MyLibraryTests
declaration.
At the top of window, where it says MyLibrary > My Mac, change My Mac to Generic iOS Device or any iOS Simulator that you may happen to have installed.
Build.
Now, to prove that this works, import the package into an iOS project. You will be able to import MyLibrary
and create a MyLibrary()
object and obtain its view
.
Personally, I don't think that's a very good way to develop a package for iOS; I would rather add the package to an existing project, so that there's a test bed where I can actually run the code immediately. Still, this proves that you can create a stand-alone package and compile it for iOS.
Upvotes: 3