赞
踩
访问 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 如下
- let a = 2
- let b = 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 如下
- import Foundation
-
- var i:Int = 0
- if CommandLine.arguments.count > 1 {
- let str = CommandLine.arguments[1]
- let number = NumberFormatter().number(from: str)
- i = number!.intValue
- } else {
- print(" usage: fibonacci.exe n ")
- exit(0)
- }
- // 计算 斐波那契数列(Fibonacci sequence)
- func fib(n: Int) -> Decimal {
- if n <= 0 { return 0 }
- var x: Decimal = 0, y: Decimal = 1
- for _ in 1..<n {
- (x, y) = (y, y + x)
- }
- return y
- }
- // 调用示例
- print("fib(\(i))=", fib(n:i))
编写 compile.bat 如下
- @echo off
- SET SDKROOT=D:\Swift\Platforms\5.10.0\Windows.platform\Developer\SDKs\Windows.sdk
- 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 如下
- import WinRT
-
- @main
- class HttpClientDemo {
- public static func main() async throws {
- var str: String
- if CommandLine.arguments.count > 1 {
- str = CommandLine.arguments[1]
- } else {
- print(" usage: HttpClient.exe url ")
- exit(0)
- }
- try RoInitialize()
-
- let httpClient = try Windows.Web.Http.HttpClient();
- let requestUri = try Windows.Foundation.Uri(uri: str);
- let httpResponse = try await httpClient.Get(uri: requestUri)!;
- let _ = try httpResponse.EnsureSuccessStatusCode();
- let httpResponseBody = try await httpResponse.Content!.ReadAsString();
- print(httpResponseBody);
- }
- }
swift build -c debug --arch x86_64
或 swift build -c release --arch x86_64
swift run --help
swift run HttpClient http://www.baidu.com
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。