Using Apple programming language in Windows Getting started with Swift development
To write apps for macOS or iOS, it is recommended to develop with Swift. Basically, the programming language is also available for Windows, but does not offer as many possibilities here as under macOS and Linux.
Companies on the topic
Xcode on macOS is the standard programming environment for Swift, but the possibilities on Windows are improving.
(Image: Joos / Apple)
Who develops with the Pogrammiersprache Swift, does not really get around Linux or macOS. No wonder, the focus of Swift is clearly in the Apple world. Apple itself now programs most of its apps with Swift.
If you own a Mac, the entry into Swift will be easier without question. For development on an Apple operating system, it is recommended to download and install the development environment (IDE) Xcode. Version 12 of Xcode supports macOS 11. x (Big Sur) and includes Swift in version 5.3.
As Apple reveals in its own blog, Swift can also be used under Windows. Together with Visual Studio 2019 and Visual Studio Code, it is easily possible to develop Swift apps under the right conditions.
With Swift for Windows, the compiler is available in Windows, but the Swift libraries can also be used in Windows. Dispatch, Foundation and XCTest can also be used in Windows. With these libraries it is quite possible to develop applications for Windows.
This makes sense, for example, for developers who want to keep it open to be able to work with Windows in parallel with macOS. The possibilities of Swift for Windows can be seen with the calculator app on Github.
Prepare Visual Studio 2019 and Swift for joint operation
For the operation of Swift on Windows, the Windows 10 toolchain for Swift is first downloaded. In addition, Visual Studio 2019 is required at least as a free community edition on the computer. After installing the necessary components for Visual Studio 2019, the Swift toolchain for Windows can be installed.
For development with Swift, Visual Studio should still select “Desktop Development with C++” in the “Desktop and Mobile Devices” section of “Workloads”. This setting can be made up at any time. To do this, the Visual Studio 2019 installer is started and clicked on “Change”. Options can then be added if Visual Studio is already installed on the computer, for example.
In the case of” Individual components”, the view can be filtered by searching for the term” Windows”. Here the two options “C++-Cmake Tools for Windows” and the “Windows 10 SDK 10.0.18362.0” should be selected. In addition, you should search for “Windows Universal C Runtime” and install the corresponding component.
Now the Swift Toolchain for Windows can be installed. Subsequently, the” x64 Native Tools Command Prompt for VS2019 “from the” Visual Studio 2019 program group ” is required. This tool can be started via the right-click context menu with administrative rights.
In addition, the following copy actions are useful so that Swift can be used. Before copying, it should be checked in Windows Explorer whether the directories are correct. When updating Visual Studio 2019, the version numbers of the directories used also change.
Copy %SDKROOT%usrshareucrt.modulemap "C:Program Files (x86)Windows Kits10Include10.0.18362.0ucrtmodule.modulemap"
Copy %SDKROOT%usrsharevisualc.modulemap "C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.28.29333includevisualc.modulemap"
Copy %SDKROOT%usrsharevisualc.apinotes "C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.28.29333includevisualc.apinotes"
Copy %SDKROOT%usrsharewinsdk.modulemap "C:Program Files (x86)Windows Kits10Include10.0.18362.0umwinsdk.modulemap"
To test Swift in Windows, it is enough with the editor in Windows 10 (notepad.exe) a file named “hello.swift”. Ideally, a folder should be created on the hard disk of the computer in the root directory in which you can work with Swift.
For working with Swift, it may be useful in the long run to use an editor such as Visual Studio Code. A first Swift test program can be stored as code in the text file, for example:
The program can then be tested in the “x64 Native Tools Command Prompt for VS2019”. For this purpose, however, another variable is necessary, which is set with the following command:
set SWIFTFLAGS=-sdk %SDKROOT% -resource-dir %SDKROOT%usrlibswift -I %SDKROOT%usrlibswiftwindows
Here, too, the corresponding directories should first be verified. The variable must be reset each time x64 Native Tools Command Prompt for VS2019 is started. The written Swift program can then be compiled with the Swift compiler:
Swiftc %SWIFTFLAGS -emit-executable -o hello.exe hello.swift
The Compiler is started in the x64 Native Tools Command Prompt from the directory where the source code for the Swift program.
Getting started in Swift with macOS is best
To start with Swift in macOS, the easiest way out of the app store in macOS is the Xcode development environment. install. Xcode can be used to develop programs for macOS and iOS. This is the standard IDE for the development of Apple programs.
To start with Swift in Xcode, a new project is created in Swift using the menu entry “FileNewProject”. After that, it is still necessary to create a new so-called “playground”. Here you enter the source code that belongs to the created project. The option is available in Xcode via “FileNewPlayground”.
When creating the playgrounds, it is already determined on which basis the program is developed. In the Playground, you can, for example, enter …
… output a line of text. The execution starts by clicking on the blue icon or starting the program in the lower area of Xcode.
For a good introduction to programming with Swift, it may be useful to download the example playground for developers and load it into Xcode with “FileOpen”. Then the commands from the tutorial are available in Xcode. The player character in the lower area executes the commands of the tutorial.