First and foremost, I hope you guys have the Swift environment in place for running and programming. If not, check out these following posts:
*Step 1 — set up Raspbian OS
*Step 2 — Set up Swift environment on Raspbian
There are many ways to write a program for Raspberry Pi.
The simplest way is you will have a keyboard, a mouse, a monitor plug directly in the Raspberry board, or just simply connect it over SSH remotely. Use command-line editors likenano,vim, whatever. Create a swift file, code it, then compile and run the program. Everything can be done interminal.
Having that said, working in an elaborate project. I don’t think that is a good way to go. I’ve come up with several ideas, researched and ended up with gettingXCode handshaking with Raspberry Pi.
That is amazing for guys who use a MAC OS machine. We’re able take advantages ofXCode, make development workflow more productivity, conveniently and much more fun. This is the goal I like to share in this post.
Note: The tutorial was done on the following environment.
* MAC: MAC OS Mojave 10.14.12,XCode 10.1
* Raspberry: Raspberry Pi 3B+, Raspbian Strech Lite OS, Swift 4.1.1
First of all, We should know how to create a project with the help of Swift tools. I’m just showing you the steps on MAC.
On your MAC, open the terminal. Make a directory, and jump in it
mkdir swift_programcd swift_program
Create the swift project
swift package init --type executable
Executable type creates a executable program, you can try types like empty|library|executable|system-module later
Ok, the project has been created. Now compile and run it.
swift compileswift run
You can call for compile and run separately, or just callswift run. It will automatically compile and run right after
Or you can just run the program which has been compiled by adding this option -skip-run
After all, you should seeHello World!
To keep this post short, I’m not gonna explain the directory structure which has been generated. You can learn more here
In order to open the project, We need aXCode project file which has .xcodeproj extension.
Simply generate the project file by the following command
swift package generate-xcodeproj
Double click to open the project in Xcode.
Basically,XCode cannot compile program for Raspberry on MAC OS, because of CPU arch, OS kernel, libraries and stuff. What We do here is ask Raspberry to compile code.
The mechanism includes 2 phases:
In order to achieve these, We need a script to do this. I need to say thankThomas Paulmann , He has the idea implemented and package it perfectly. It works like a boss. Check it out his project hereSwish for more detail.
→ See the full setup guide of Swishhere
Run this command to installSwish on your MAC
brew install thomaspaulmann/homebrew-formulae/swish
Then you need to pair your MAC with Raspberry, so that everytime you callssh, it will not ask you again for password.
Firstly, Generate public key
ssh-keygen -t rsa
Put it into Raspberry
cat ~/.ssh/id_rsa.pub | ssh @ "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
Check ssh pairing again.
ssh user@hostname
Try this twice. In the second time, it should not ask you for the password.
In this part, We integrates Swish into XCode
Firstly, We need to add an externalbuild system target to project. Follow steps in the following pictures
Secondly, We need to create scheme for the target We has added. Follow these steps
Now, We need to set swish with the Raspberry user and ip address in the build target
Note:
— The Swift version on Raspberry is Swift 4.1.1. So make sure theswift-tools-version inPackage.swift is 4.1
— The Raspberry Pi must be on, and accessible via the given user/ip address. (Use ssh to check accessibility)
When everything is in place, to build the project. You need to select the target “RemoteBuild”, choose the platform as a MAC (In fact, It triggers the build script to build on Raspberry Pi)
Then click play Icon to compile, or use the shortcut keyCmd + B on MAC
After all, you should see “Build succeeded” message.
Now, you connect to the Raspberry Pi by ssh.
The project locates at/home/pi/Swish/swift_program
Change to directory the project directory, and use the following command to run the project
swift run --skip-build
Come to this point, We’re all done.
At this time, I seeXCode is the best candidate for Swift development. However,XCode can be so advanced for developers who are not familiar. There is another option, Visual Studio Code. This is a well-known, handy tool, it is strong enough to write a decent Swift program.
Lean more here ->Compile Swift for Raspberry Pi by Visual Studio Code
Published
Originally published athttp://swiftreviewercom.wordpress.com on December 24, 2018.
Been working on iOS, tvOS, Swift, and Objective-C since once upon a time. I'm just a little iOS developer, but like to mess with everything.