In order to keep this tutorial short, I assume that you have had Raspbian OS, Swift and Swish in place, otherwise please check out these following posts:
* Set up Raspbian Strech Lite OS
* Set up Swift 4.1 environment
* Setup Swish and compile source in XCode
Visual Studio Code is free, and available on Window, Mac and Linux. But this tutorial limits for the guys who use Mac or Linux. Because I’m not sure if there are rsync, ssh on Window or not. If available, not sure they function as same as on Mac and Linux.
The mechansim which compiles swift sources for Raspberry Pi on Visual Studio Code is as same as XCode. Visual Studio Code will sync up source code to Raspberry Pi, then send compilation command over ssh to have them compiled on Raspberry Pi.
The benefit here is you take advantage of Visual Studio Code instead of using a boring command-line editor.
Ok, let get started by create a new project named “Raspberry_Pi_In_Swift” by these following commands:
Create a new folder
Jump in the folder
Create a new project
swift package init --type executable
After all, open the project in Visual Studio Code in menu File > Open or Cmd + O and choose the Raspberry_Pi_In_Swift folder.
You will see the Swift project opened like the following picture.
Config build task
In visual studio code, open menu item Terminal > Configure Tasks, then select “Create tasks.json from template” and choose “Others – example to run an arbitrary external command”
Now the task is created, set a label for it. It can be anything but choose an appropriate label, for example “Remotely build on Raspberry Pi”.
In the command, put swish command in there with the raspberry pi’s user and ip address. Then save and close it.
In the menu, choose Terminal > Configure Default Build Task, and choose the task which you have created earlier.
Before building, make sure in Package.swift, swift-tools-version version must be appropriate to the swift on the Raspberry Pi. It should be 4.1 for Swift 4.1.x
Here We go. Choose Terminal > Run Build Task or Shift + Cmd + B to build the project. You will see the following console output.
On the Raspberry Pi end, the project is located at home/pi/swish/Raspberry_Pi_In_Swift
Jump in the project folder by
and run the compiled program
swift run --skip-build
Visual Studio Code is free, simple and lightweight editor. It’s litterally friendly to beginner. However, in term of features, functionalities, XCode is many times better than Visual Studio Code.
The XCode has better IntelliSense for Swift. It also supports running and debugging Swift.
In order take advantages of XCode, I will share you some useful stragegies to help write code for Raspberry Pi which you can run and debug straight on MAC/XCode, so stay tuned