tar*_*leb 3 linux swift swift5.1
当我尝试使用Foundation的 struct URLRequest 时,在使用 swift 5.1.1 进行编译时出现错误。相同的代码适用于 swift 5.0.1。
示例:给定带有内容的文件 Foo.swift
import Foundation
print(URLRequest.self)
Run Code Online (Sandbox Code Playgroud)
使用 Swift 5.0.1 我们得到
$ docker run --rm -v "$PWD:/app" swift:5.0.1 sh -c \
'swiftc /app/Foo.swift && ./Foo'
URLRequest
Run Code Online (Sandbox Code Playgroud)
但是随着 5.1.1
$ docker run --rm -v "$PWD:/app" swift:5.1.1 sh -c \
'swiftc /app/Foo.swift && ./Foo
Foo.swift:2:7: error: use of unresolved identifier 'URLRequest'
print(URLRequest.self)
^~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
我似乎找不到任何提及 Foundation 相关更改的内容,而且https://github.com/apple/swift-corelibs-foundation 上的源代码看起来也很稳定。
这里发生了什么,是否有解决方法?
tar*_*leb 12
这是由于最近将网络相关对象移动到新的FoundationNetworking模块中造成的。Darwin 上不存在新模块,因此必须使用预处理器命令才能使代码在所有支持的平台上工作:
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
print(URLRequest.self)
Run Code Online (Sandbox Code Playgroud)
上面给出的两个 docker 命令都可以很好地编译此代码。
| 归档时间: |
|
| 查看次数: |
810 次 |
| 最近记录: |