更新
// I have somthing like this declared outsIDe class// I put question mark wrapper since I don't kNow when this session might have a valuevar url = "\(self.session?.APIURL)/API/products.Json"// private sessionprivate var _session:Session?class MyClass{ . . . // the value of APIURL depends on session,session has optional value and declared as // custom lazy loaded var session var session:Session? { get { if _session == nil { _session = // fetch from coredata store if there is an active session. Might return nil // if no active session if _session == nil { // I just print "No active session" } } // return _session may or may not contain any value return _session } }}
当会话具有值时,url具有值:
Optional("my_API_url_here")/API/products.Json您可以使用 ?? (null coalescing operator)打开它并提供默认值(如果它是nil)
let sessionAPIURL = self.session?.APIURL ?? ""var url = "\(sessionAPIURL)/API/products.Json"总结
以上是内存溢出为你收集整理的Swift:可选值中的可选文本全部内容,希望文章能够帮你解决Swift:可选值中的可选文本所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)