当前位置:   article > 正文

Swift:在 Win10 上编程入门_swift windows

swift windows

访问 https://swift.org/download/
找到 Windows 10:x86_64
下载 swift-5.10-RELEASE-windows10.exe  大约490MB
建议安装在 D:\Swift\ ,安装后大约占2.56GB

官网文档:https://www.swift.org/documentation/

中文教程:The swift programming language 中文版

运行  cmd

swiftc -v
Swift version 5.10 (swift-5.10-RELEASE)
Target: x86_64-unknown-windows-msvc

cd D:\Swift\

mkdir test ; cd  test

用 Notepad++ 编写  hello.swift  如下

  1. let a = 2
  2. let b = 3
  3. print("a+b=", a+b)

SET SDKROOT=D:\Swift\Platforms\5.10.0\Windows.platform\Developer\SDKs\Windows.sdk

可以在环境变量中设置,编译执行  swiftc hello.swift -o hello.exe -sdk %SDKROOT%

运行  hello.exe

编写  fibonacci.swift  如下

  1. import Foundation
  2. var i:Int = 0
  3. if CommandLine.arguments.count > 1 {
  4. let str = CommandLine.arguments[1]
  5. let number = NumberFormatter().number(from: str)
  6. i = number!.intValue
  7. } else {
  8. print(" usage: fibonacci.exe n ")
  9. exit(0)
  10. }
  11. // 计算 斐波那契数列(Fibonacci sequence)
  12. func fib(n: Int) -> Decimal {
  13. if n <= 0 { return 0 }
  14. var x: Decimal = 0, y: Decimal = 1
  15. for _ in 1..<n {
  16. (x, y) = (y, y + x)
  17. }
  18. return y
  19. }
  20. // 调用示例
  21. print("fib(\(i))=", fib(n:i))

编写 compile.bat  如下

  1. @echo off
  2. SET SDKROOT=D:\Swift\Platforms\5.10.0\Windows.platform\Developer\SDKs\Windows.sdk
  3. swiftc %1.swift -o %1.exe -sdk %SDKROOT%

编译执行  compile.bat  fibonacci

运行 fibonacci.exe 200

fib(200)= 280571172992510140037611932413038677186000  这个结果溢出了。

下一篇 python fibonacci.py 200

fib(200)= 280571172992510140037611932413038677189525 这个结果是对的。

从 https://github.com/ericsink/SwiftWinRT  下载 SwiftWinRT-main.zip

cd D:\Swift\test\SwiftWinRT-main\WinRT
swift build

Microsoft.WindowsAppRuntime.Bootstrap.lib 在 D:\Swift\test\SwiftWinRT-main\Samples\ 中

从 GitHub - thebrowsercompany/swift-winui: Swift language bindings for WinUI3

下载  swift-winui-main.zip  请看 README.md ; 

从 Windows商店下载 WinUI 3 Gallery

将 Swift软件包构建为通用二进制文件:

cd D:\Swift\test\SwiftWinRT-main\Samples\HttpClient

编辑  SwiftWinRT-main\Samples\HttpClient\Sources\HttpClient\main.swift  如下

  1. import WinRT
  2. @main
  3. class HttpClientDemo {
  4. public static func main() async throws {
  5. var str: String
  6. if CommandLine.arguments.count > 1 {
  7. str = CommandLine.arguments[1]
  8. } else {
  9. print(" usage: HttpClient.exe url ")
  10. exit(0)
  11. }
  12. try RoInitialize()
  13. let httpClient = try Windows.Web.Http.HttpClient();
  14. let requestUri = try Windows.Foundation.Uri(uri: str);
  15. let httpResponse = try await httpClient.Get(uri: requestUri)!;
  16. let _ = try httpResponse.EnsureSuccessStatusCode();
  17. let httpResponseBody = try await httpResponse.Content!.ReadAsString();
  18. print(httpResponseBody);
  19. }
  20. }

swift build -c debug  --arch x86_64
或 swift build -c release --arch x86_64

swift run --help
swift run HttpClient http://www.baidu.com

推荐观看:使用WinSDK | WinUI | Swift-WinRT构建Swift Windows新闻应用程序

推荐阅读:Swift 编程入门(非常详细)从零基础入门到精通

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/807121
推荐阅读
相关标签
  

闽ICP备14008679号