Exploring Swift On Windows
Swift is most commonly known for targeting Apple's platforms, though it also supports other platforms including Linux and Windows.
I've been using Swift to develop iOS apps for work for a while now, and have found the language experience to be quite a pleasant one. With the relatively recent release of version 6 of the Swift programming language, I decided to explore its support for Windows. This was partly because I've been reading some of the documentation regarding C++ inter, which seems like a potentially very useful feature.
Installation
Installation turned out to be really easy, thanks to the WinGet package. This is the recommended installation method from the Swift team too, as per the official "Install Swift" page.
winget install -e --id Swift.Toolchain
As of the time of writing, this installs v6.0.2 of the Swift toolchain.
Tooling
The tooling experience seems pretty good. I've been using Visual Studio Code. The Browser Company posted a pretty comprehensive post about Swift Tooling: Windows Edition, which goes into using sourcekit-lsp
via the Swift extension.
Platform Integration
Swift includes a WinSDK
module, which allows direct access to the Win32 API and more, as explained in the Swift team's blog post Swift Everywhere: Using Interoperability to Build on Windows.
As such, it's easy to start calling native APIs without having to write any kind of definitions for native methods (as you might in C#).
You do have to do some work when working with strings to get them into a format that's used by Win32, but that's a pretty trivial task with an extension like this:
import WinSDK
extension String {
public var wideString: [UInt16] {
return self.withCString(encodedAs: UTF16.self) { buffer in
Array<UInt16>(unsafeUninitializedCapacity: self.utf16.count + 1) {
WinSDK.wcscpy_s($0.baseAddress, $0.count, buffer)
$1 = $0.count
}
}
}
}
Other Projects
While experimenting I found the swift-win32 project, which is a cool project in itself to provide a Swift library to write desktop applications in Swift. If nothing else, it's full of examples of calling the native SDK from Swift.