Swift coding in visual studio code

i0S Swift Issue

Question or problem in the Swift programming language:

Is there any possibility to write swift code and preview in Visual Studio Code (windows coding app)? I see in the guide for swift coding that requires ios system but i have just windows for the moment.

Can I code swift in Windows?

How to solve the problem:

Solution 1:

Yes, you can code Swift on Windows. Check out RemObjects Silver which gives you a Swift compiler in Visual Studio 2015. Visual Studio Express is free, but I don’t know if it works with Silver.

Swift itself is just a language. In order to build an iOS app, you need frameworks and libraries like Foundation and UITouch (which can be written in any language, in this case: C, Objective-C and C++). Apple has open sourced many Foundation classes and ported them to Linux but there are lot of stuffs that are not fully implemented or there are bugs in Linux that don’t exist in OS X. There’s no official Windows port. I doubt Apple will ever open source UITouch at all.

Silver can target .NET (and I guess by extension its cross-platform cousin Mono), Java or Sugar (vendor’s proprietary framework). If you want to use Swift on Windows, yes, you can, but don’t expect to build an iOS app on Windows. If you are serious about iOS development, buy a Mac, even the $500 Mac Mini can go a long way. If you only want to play with Xcode, assemble a Hackintosh or a virtual machine so that you can run OS X.


Update June 2020: at WWDC 2020, Apple announced Swift 5.3 will expand to include support of Windows and more Linux platforms:


Swift 5.3 is a release meant to include significant quality and performance enhancements. In addition, this release will expand the number of platforms where Swift is available and supported, notably adding support for Windows and additional Linux distributions.

Solution 2:

Install Swift for windows into c:\swift

Install vs code plugin code-runner

Set vs code – file – preferences – settings – user settings

{
   "code-runner.executorMap": {
        "swift": "C:\\swift\\setPath.bat && cd $dir && swiftc -swift-version 4 $fileName -o $fileNameWithoutExt.exe && $fileNameWithoutExt.exe",
    },
}

Open file .swift.
Run code (Ctrl+Alt+N)

Solution 3:

If Ramil’s answer does not work for you…

Then for the settings.json in vscode, use instead:

{
    "code-runner.executorMap": {
        "swift": "cd $dir && C:\\Swift\\bin\\swiftc.exe -swift-version 4 $fileName -o C:\\Swift\\RuntimeEnv\\$fileNameWithoutExt.exe && C:\\Swift\\bin\\run.bat && C:\\Swift\\RuntimeEnv\\$fileNameWithoutExt.exe",
    },
}
Additionally, for a more aesthetically pleasing running of code after compilation:

Inside the run.bat file found in C:\Swift\bin, replace the

if %SFW_ARGC% == 0 (
  echo Run a program in the Swift runtime environment
  echo.
  echo Usage: run ^
)

with a simple “echo.”

Hope this helps!