Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create and build a swift package

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?

like image 432
Just a coder Avatar asked Sep 17 '25 07:09

Just a coder


1 Answers

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
like image 162
Mojtaba Hosseini Avatar answered Sep 19 '25 20:09

Mojtaba Hosseini