如何解包返回的字符串:
(可选(可选"蓝色"))
var cityName = String()
if let cityAnno = annotation as MGLAnnotation! {
cityName = String(stringInterpolationSegment: cityAnno.title!)
}
cityLabelName.text = ("\(cityName), \(county)")
Run Code Online (Sandbox Code Playgroud)
cityLabelName打印为(可选"纽约")
如何检查字典中是否存在密钥?我的字典是类型的[Type:Type?].
我不能简单地检查dictionary[key] == nil,因为这可能是由于价值所致nil.
有任何想法吗?
假设我有一段这样的代码:
let x: Int? = 10
let y: Any = x
Run Code Online (Sandbox Code Playgroud)
现在我想把y转换成Int?:
let z = y as Int? // Error: Cannot downcast from 'Any' to a more optional type 'Int?'
Run Code Online (Sandbox Code Playgroud)
这是不可能还是有另一种方式?
斯威夫特 5.1。考虑以下。
let x: Any? = nil
let y: Any = x
print("x \(x)") // x nil
print("type(of: x) \(type(of: x))") // type(of: x) Optional<Any>
print("x == nil \(x == nil)") // x == nil true
print("y \(y)") // y nil
print("type(of: y) \(type(of: y))") // type(of: y) Optional<Any>
print("y == nil \(y == nil)") // y == nil false
Run Code Online (Sandbox Code Playgroud)
我们有两个变量,设置为相同的东西——nil。他们打印相同,他们的类型打印相同 - 但一个== nil和另一个没有。
Any,人们怎么会发现它实际上为零?