Swift 协议和扩展部分的公共属性出现快速浏览、编译器错误
我是斯威夫特的新手,我要穿过操场去参加比赛。我正在按原样执行代码,但在运行以下块时遇到编译器错误:Swift 协议和扩展部分的公共属性出现快速浏览、编译器错误,swift,Swift,我是斯威夫特的新手,我要穿过操场去参加比赛。我正在按原样执行代码,但在运行以下块时遇到编译器错误: extension Int: ExampleProtocol { var simpleDescription: String { return "The number \(self)" } mutating func adjust() { self += 42 } } print(7.simpleDescript
extension Int: ExampleProtocol {
var simpleDescription: String {
return "The number \(self)"
}
mutating func adjust() {
self += 42
}
}
print(7.simpleDescription)
协议定义如下:
protocol ExampleProtocol {
var simpleDescription: String { get }
mutating func adjust()
}
编译器错误为:
error: Protocols and Extensions.xcplaygroundpage:41:9: error: property 'simpleDescription' must be declared public because it matches a requirement in public protocol 'ExampleProtocol'
var simpleDescription: String {
^
Protocols and Extensions.xcplaygroundpage:41:9: note: mark the property as 'public' to satisfy the requirement
var simpleDescription: String {
^
public
我尝试在simpleDescription
前面添加public
,但随后出现了一个意外模式
错误
有人知道这里发生了什么吗?虽然我的Xcode版本正确地为12.1,但我的本地Swift版本为5.2.4(本教程假设为5.3)。我为Xcode 12安装了命令行工具,它将我的Swift更新为5.3,现在代码按预期编译。我使用了
xcrunswift-version
来查看使用的是哪个版本的swiftxcode。您使用的是哪个版本的Xcode?(因为它在Xcode 12.1游乐场上运行良好)您是否对游乐场文件进行了任何意外修改?是否意外地将public
添加到协议声明中?i、 e.public protocol ExampleProtocol{
?虽然我使用的是Xcode 12.1,但我的本地swift版本是5.2.4。我安装了用于Xcode 12的命令行工具包,该包将我的swift更新为5.3,并将其修复!感谢您为我指明了正确的方向