I wanted to write a post on commandline swift projects that span more than one file. This post covers a hello world type example that spans three files and prompts for input. Also covered are a file wrapper for multi file swift scripts and compiled command line executables.
Also see the post on Getting Started With Command Line Swift.
First, we will create a multi file project.
Add the following to main.swift.
Add the following to input_prompt.swift.
Add the following to hello.swift.
To execute main.swift and the support files, create a wrapper script. Add the following to hello_user.sh.
Make the wrapper executable.
The wrapper script can be run from the command line.
Alternatively, compile the files into an executable with swiftc.
One of the files in this project imports foundation, so xcrun -sdk macosx swiftc needs to be used instead of swiftc.
The executable can be run from the command line.
- Swift, Getting Started With Command Line Swift
- Swift, Is there a way to have a Swift script use multiple files
- Swift, How to include .swift file from other .swift file in an immediate mode?
- Swift, Scripting in Swift is Pretty Awesome
- Swift, How to write small command line scripts in Swift
- Swift, Minimal Swift command line Hello World
- Swift for CLI tools
- Swift, Functions
- Swift, Input from the keyboard in command line application
- Swift, ‘Use of Unresolved Identifier’ in Swift
- Swift, print without newline in swift 2.0
- Swift, Update current line with command line tool in Swift
- Swift, Does swift has trim method on String?