【发布时间】:2019-09-24 22:30:28
【问题描述】:
我想我可能错过了它的工作原理,但是我有一个类需要在它的几个方法中使用全局可选值,现在我在每个方法中解包它,但我想我可以解包值在初始化()中。我做错了还是现在应该如何工作? - 谢谢。
let iCloudPath = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents")
class iCloudManager {
init() {
guard let iCloudPath = iCloudPath else { return }
}
function1(){
// uses iCloudPath but returns 'Value of optional type 'URL?' must be unwrapped to a value of type 'URL''
}
function2(){
// uses iCloudPath but returns 'Value of optional type 'URL?' must be unwrapped to a value of type 'URL''
}
}
【问题讨论】:
-
nil的返回值在这里究竟是什么意思?
标签: swift function optional init guard