简体   繁体   English

Visual Studio 代码中的 Swift 编码

[英]Swift coding in visual studio code

Is there any possibility to write swift code and preview in Visual Studio Code (windows coding app)?是否有可能在 Visual Studio Code(Windows 编码应用程序)中编写 swift 代码和预览? I see in the guide for swift coding that requires ios system but i have just windows for the moment.我在需要 ios 系统的快速编码指南中看到,但我目前只有 Windows。

Can I code swift in Windows?我可以在 Windows 中快速编码吗?

Yes, you can code Swift on Windows.是的,您可以在 Windows 上编写 Swift 代码。 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.查看RemObjects Silver ,它为您提供了 Visual Studio 2015 中的 Swift 编译器。Visual Studio Express 是免费的,但我不知道它是否适用于 Silver。

Swift itself is just a language. Swift 本身只是一种语言。 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++).为了构建 iOS 应用程序,您需要诸如 Foundation 和 UITouch 之类的框架(可以用任何语言编写,在这种情况下:C、Objective-C 和 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. Apple 已经开源了许多 Foundation 类并将它们移植到 Linux,但是有很多东西没有完全实现,或者 Linux 中存在 OS X 中不存在的错误。没有正式的 Windows 端口。 I doubt Apple will ever open source UITouch at all.我怀疑 Apple 是否会开源 UITouch。

Silver can target .NET (and I guess by extension its cross-platform cousin Mono), Java or Sugar (vendor's proprietary framework). Silver 可以针对 .NET(我想通过扩展它的跨平台表亲 Mono)、Java 或 Sugar(供应商的专有框架)。 If you want to use Swift on Windows, yes, you can, but don't expect to build an iOS app on Windows.如果您想在 Windows 上使用 Swift,是的,您可以,但不要期望在 Windows 上构建 iOS 应用程序。 If you are serious about iOS development, buy a Mac, even the $500 Mac Mini can go a long way.如果您对 iOS 开发很认真,请购买 Mac,即使是 500 美元的 Mac Mini 也能走很长的路。 If you only want to play with Xcode, assemble a Hackintosh or a virtual machine so that you can run OS X.如果你只想玩 Xcode,组装一个 Hackintosh 或一个虚拟机,这样你就可以运行 OS X。


Update June 2020: at WWDC 2020, Apple announced Swift 5.3 will expand to include support of Windows and more Linux platforms: 2020 年 6 月更新:在 WWDC 2020 上,Apple 宣布Swift 5.3将扩展以支持 Windows 和更多 Linux 平台:

Swift 5.3 is a release meant to include significant quality and performance enhancements. Swift 5.3 是一个旨在包含重大质量和性能增强的版本。 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.此外,此版本将扩展可使用和支持 Swift 的平台数量,特别是增加了对 Windows 和其他 Linux 发行版的支持。

There is official support for Swift on Windows 10. It works with Visual Studio 2019 (not VS Code). Swift 在 Windows 10 上有官方支持。它适用于 Visual Studio 2019(而不是 VS Code)。 Install Swift on Windows在 Windows 上安装 Swift

Install Swift for windows into c:\\swiftSwift for windows安装到 c:\\swift

Install vs code plugin code-runner安装 vs code 插件code-runner

Set vs code - file - preferences - settings - user settings设置vs代码-文件-首选项-设置-用户设置

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

Open file .swift.打开文件 .swift。 Run code ( Ctrl + Alt + N )运行代码( Ctrl + Alt + N

If Ramil's answer does not work for you...如果拉米尔的回答对你不起作用......

Then for the settings.json in vscode, use instead:然后对于 vscode 中的 settings.json,改用:

{
    "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在 C:\\Swift\\bin 中找到的 run.bat 文件中,替换

if %SFW_ARGC% == 0 (
  echo Run a program in the Swift runtime environment
  echo.
  echo Usage: run ^<swift-compiled-exe-file^>
)

with a simple "echo."用一个简单的“回声”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM