gpt4 book ai didi

swift - 我尝试检查变量类型的代码有什么问题?

转载 作者:行者123 更新时间:2023-11-28 05:34:48 25 4
gpt4 key购买 nike

<分区>

我尝试检查变量类型的代码有什么问题?

以下代码会产生错误“'is' test is always true”。请注意,我不想将 p 设置为一个值,因为它可能为 nil,因此使用了可选值。

import Foundation

var p:String?

if p as String? {
println("p is a string type")
}
else {
println("p is not a string type")
}

现在如果我针对 String 类型进行测试,它甚至不会编译:

 import Foundation

var p:String?

if p as String {
println("p is a string type")
}
else {
println("p is not a string type")
}

这是编译器错误吗?如果不是,我做错了什么?

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