It's not just Foundation, but you can import AppKit or even SwiftUI. Have you noticed the new shebang? If you have multiple Swift versions installed on your system, you can use the env shebang to go with the first one that's available in your PATH. Just a few lines of code, but it contains so much info. Yes, you can say that this script is completely useless, but in my opinion it's an amazing demo app, since it covers how to check command line arguments ( CommandLine.arguments), it also shows you how to wait ( dispatchMain) for an async task, such as a HTTP call through the network using the URLSession API to finish and exit using the right method when something fails ( fatalError) or if you reach the end of execution ( exit(0)). If you call this example with a URL that can return a list of todos it'll print a nice list of the items./main.swift joined(separator: "\n"))įatalError( "Error: \(error. With the help of Foundation we can build quite useful or completely useless command line utilities. The good news is that we can import all sort of system frameworks that are part of the Swift toolchain. Thanks to the program-loader mechanism and of course the Swift interpreter we can skip an extra step and run our single-source Swift code as easy as a regular shell script. main.swift file it'll print out the famous "Hello world!" text. The good news that we can take this one step further by auto-invoking the Swift compiler under the hood with a shebang). In Swift if we want to print something, we don't even have to import the Foundation framework, we can simply compile and run this piece of code by running the following: swiftc main.swift # compile main.swiftĬhmod +x main # add the executable permission Consider the most simple Swift program that we can all imagine in a main.swift file: print( "Hello world!") Swift compiler 101, you can create, build and run a Swift file using the swiftc command.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |