【发布时间】:2019-09-12 13:24:28
【问题描述】:
在 Swift 中,我有一个返回某种对象的函数。该对象是可选的。当它不存在时,我想我应该返回nil,但 Swift 禁止我这样做。以下代码不起作用:
func listForName (name: String) -> List {
if let list = listsDict[name] {
return list
} else {
return nil
}
}
上面写着:error: nil is incompatible with return type 'List'
但我不想返回空 List 对象之类的东西,当 optional 为空时我想什么都不返回。该怎么做?
【问题讨论】:
-
这是因为返回类型
List不是可选的,通常将返回更改为List? -
@CarpenterBlood 谢谢
标签: swift