Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import module like "UIKit" when using "swiftc" command to compile swift file?

Tags:

terminal

swift

I create a swift file (test.swift):

import UIKit

func test() {
    let view = UIView()
    print(view)
}

Then run the command swiftc test.swift in terminal. It outputs

error: no such module 'UIKit'

How to import the 'UIKit' module?

like image 546
a_tuo Avatar asked Oct 06 '17 09:10

a_tuo


People also ask

How do I compile swift in Xcode?

If you've installed the latest version of Xcode you should have a swift command available in your terminal, which launches a Swift REPL 1. You can run the Swift REPL from the command line and enter Swift code directly into it. Whenever you enter valid Swift code in the REPL, it will immediately compile and run it.


1 Answers

You need to link sdk and specify what target you building for:

swiftc input.swift 
    -o output.o 
    -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk 
    -target arm64-apple-ios12.0
like image 99
ManWithBear Avatar answered Oct 04 '22 01:10

ManWithBear