macOS文档处理:从代码实现到用户界面搭建
1. 准备工作与 guard 关键字
在开始文件的加载和保存系统之前,会用到 err 方法,示例如下:
// Load the text data as RTF
guard let documentText = NSAttributedString(rtf: documentTextData,
documentAttributes: nil) else {
throw err(.cannotLoadText)
}
这里使用了 guard 关键字,它是在Swift 2中引入的,能帮助我们避免编写两种令人头疼的代码: if 嵌套(有时称为“厄运金字塔”)和提前返回。
1.1 if 嵌套示例
if let someObjectA = optionalA {
if let someObjectB = optionalB {
if let someObjectC = optionalC {
// Do something that relies on all three of these optionals
// having a value
}
}
}
超级会员免费看
订阅专栏 解锁全文
1219

被折叠的 条评论
为什么被折叠?



