4

I am trying to create a new swift package i did the following commands on console

  1. swift package init
  2. swift package generate-xcodeproj

This works and generates an empty project. Inside the project sample file i added just one line

Import UIKit

In Xcode, this builds correctly. But on console, when I do swift build command, I get this error

/Users/home/Desktop/TT/Sources/TT/TT.swift:1:8: error: no such module 'UIKit'
import UIKit

Is there any thing I am doing wrong?

Just a coder
  • 15,480
  • 16
  • 85
  • 138

2 Answers2

2

You should select an iOS based target to make it available:

Demo

If you leave it selecting macOS (by default), you will get the error.


Specific platform

if you want your package to be available only for specific platforms (for example only for iOS), you should specify the platform in the package.swift file:

let package = Package(
    name: "MyLibrary",
    platforms: [
        .iOS(.v9)
    ],
    products: [
,,,

Multiplatform

If you need your framework to be available on multiple platforms, don't forget to check the availability of the imported framework like:

#if canImport(UIKit)

import UIKit

// And do the rest of UIKit dependent code

#endif
Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
1

UIKit is a framework in iOS and won't be accessible.

#if canImport(UIKit)

// Code specific to platforms where UIKit is available

#endif

Related:

excitedmicrobe
  • 2,338
  • 1
  • 14
  • 30