gpt4 book ai didi

swift 1.2 : Obnoxious snippet shows Types can be shadowed by variable identifiers

转载 作者:搜寻专家 更新时间:2023-11-01 05:40:24 25 4
gpt4 key购买 nike

为什么在 Swift 中允许这样做:

// Swift 1.2, Xcode 6.4, OS X 10.10.4
// Running within an iOS App, not a Playground
func wtf() {
var String = 3
var Int = "3a"
println(String) // outputs 3
println(Int) // outputs 3a
println(Int.toInt()) // outputs nil; clearly acting as a String
// var x: String = "abc" // Not OK: 'String' is not a type
}

类型和变量绑定(bind)可以相互干扰吗?甚至基本类型?

最佳答案

在“真实”环境(不是 Playground)中,我在第一行收到一条错误消息 --> 在声明之前使用局部变量 'Int'

如果我注释掉第一行,代码会编译并在添加行以打印 xy 之后得到

Swift.String
3
Optional(3)
abc
abc

关于 swift 1.2 : Obnoxious snippet shows Types can be shadowed by variable identifiers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31277423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com