lohenyumnam
11/24/2017 - 4:08 AM

Swift Compilation with Sublime Text 3 on macOS.md

Swift Compilation with Sublime Text 3 on macOS

Adding Swift Build System

Open Sublime Text 3 Go To Preferences > Browse Packages... Create a folder "SwiftBuilder" inside Packages directory. Add a file named Swift.sublime-build inside SwiftBuilder directory. Copy the following script in Swift.sublime-build file.

{
    "shell_cmd": "xcrun swift \"$file\"",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.swift",
}

Go to Preferences > Key Bindings-User and add the following script to it.

{
    "keys": ["super+b"],
    "command": "build",
    "context": [
        { "key": "selector", "operator": "equal", "operand": "source.swift" }
        ], 
        
        "args": {
        "build_system": "Packages/SwiftBuilder/Swift.sublime-build",
        "variant": "Build"
        }
}

You are ready to run Swift scripts from Sublime Text 3 now.

Testing the Swift Build System

  1. Create a file A.swift containing a single line println("Hello, Swift!").
  2. Press command + b and you'll be able to see the output.
  3. If you are unable to see the output, go to Tools > Build Systems and change it from Automatic to Swift, and you should be able to run Swift in ST3.