如何避免强制打开日期 .second 组件?
How to avoid to force unwrap a date .second component?
我正在使用此代码为 macOS 的文档基础应用程序创建一个文档,该文档的名称是自 Date() 引用日期以来经过的秒数。
func saveDocumentInApplicationSupport() {
do
{
// create a directory in Application Support
let fileManager = FileManager.default
let appSupportURL = fileManager.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
appSupportURL.appendingPathComponent("com.myCompany.myApp")
let directoryURL = appSupportURL.appendingPathComponent("com.myCompany.myApp").appendingPathComponent("Documents")
try fileManager.createDirectory (at: directoryURL, withIntermediateDirectories: true, attributes: nil)
// set a name for the file
let date = Date(timeIntervalSinceReferenceDate: 0) // "2001-01-01 00:00:00 +0000"
let seconds = Calendar.current.dateComponents([.second], from: date, to: Date()).second // eg. 517848179
let fileName = "\(seconds!).idoc" // avoid force unwrapping here
// eg. 517848179.idoc
// Create document
let documentURL = directoryURL.appendingPathComponent (fileName)
try document.write (to: documentURL, ofType: "com.myCompany.idoc")
}
catch
{
print("An error occured")
}
}
避免强制展开秒变量的正确方法是什么?
我认为你在追逐一条红鲱鱼。你担心这个强制解包,但你的整个代码块都被包围在一个包含多个 try
的 do
/catch
中,完全忽略错误,并且几乎没有做任何事情损害控制。
我唯一的建议是将力展开移动到 seconds
的定义:
func saveDocumentInApplicationSupport() {
do {
// create a directory in Application Support
let fileManager = FileManager.default
let appSupportURL = fileManager.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
appSupportURL.appendingPathComponent("com.myCompany.myApp")
let directoryURL = appSupportURL.appendingPathComponent("com.myCompany.myApp").appendingPathComponent("Documents")
try fileManager.createDirectory (at: directoryURL, withIntermediateDirectories: true, attributes: nil)
// set a name for the file
let date = Date(timeIntervalSinceReferenceDate: 0) // "2001-01-01 00:00:00 +0000"
let seconds = Calendar.current.dateComponents([.second], from: date, to: Date()).second!
let fileName = "\(seconds).idoc" // eg. 517848179.idoc
// Create document
let documentURL = directoryURL.appendingPathComponent (fileName)
try document.write (to: documentURL, ofType: "com.myCompany.idoc")
}
catch {
print("An error occured")
}
}
强制展开 并不普遍 "bad"。您使用 force unwrap 运算符是完全合理的,因为您 只是 将 dateComponents
定义为具有 .second
组件。在这种情况下,即使您确实使用条件绑定来 "safely" 展开您的可选,在 nil
情况下您会做什么?
let theDate = Date()
let date = Date(timeIntervalSinceReferenceDate: 0) // "2001-01-01 00:00:00 +0000"
let seconds = Calendar.current.dateComponents([.second], from: date, to: theDate).second!
等同于直接调用:
let seconds = Int(theDate.timeIntervalSinceReferenceDate)
我正在使用此代码为 macOS 的文档基础应用程序创建一个文档,该文档的名称是自 Date() 引用日期以来经过的秒数。
func saveDocumentInApplicationSupport() {
do
{
// create a directory in Application Support
let fileManager = FileManager.default
let appSupportURL = fileManager.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
appSupportURL.appendingPathComponent("com.myCompany.myApp")
let directoryURL = appSupportURL.appendingPathComponent("com.myCompany.myApp").appendingPathComponent("Documents")
try fileManager.createDirectory (at: directoryURL, withIntermediateDirectories: true, attributes: nil)
// set a name for the file
let date = Date(timeIntervalSinceReferenceDate: 0) // "2001-01-01 00:00:00 +0000"
let seconds = Calendar.current.dateComponents([.second], from: date, to: Date()).second // eg. 517848179
let fileName = "\(seconds!).idoc" // avoid force unwrapping here
// eg. 517848179.idoc
// Create document
let documentURL = directoryURL.appendingPathComponent (fileName)
try document.write (to: documentURL, ofType: "com.myCompany.idoc")
}
catch
{
print("An error occured")
}
}
避免强制展开秒变量的正确方法是什么?
我认为你在追逐一条红鲱鱼。你担心这个强制解包,但你的整个代码块都被包围在一个包含多个 try
的 do
/catch
中,完全忽略错误,并且几乎没有做任何事情损害控制。
我唯一的建议是将力展开移动到 seconds
的定义:
func saveDocumentInApplicationSupport() {
do {
// create a directory in Application Support
let fileManager = FileManager.default
let appSupportURL = fileManager.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
appSupportURL.appendingPathComponent("com.myCompany.myApp")
let directoryURL = appSupportURL.appendingPathComponent("com.myCompany.myApp").appendingPathComponent("Documents")
try fileManager.createDirectory (at: directoryURL, withIntermediateDirectories: true, attributes: nil)
// set a name for the file
let date = Date(timeIntervalSinceReferenceDate: 0) // "2001-01-01 00:00:00 +0000"
let seconds = Calendar.current.dateComponents([.second], from: date, to: Date()).second!
let fileName = "\(seconds).idoc" // eg. 517848179.idoc
// Create document
let documentURL = directoryURL.appendingPathComponent (fileName)
try document.write (to: documentURL, ofType: "com.myCompany.idoc")
}
catch {
print("An error occured")
}
}
强制展开 并不普遍 "bad"。您使用 force unwrap 运算符是完全合理的,因为您 只是 将 dateComponents
定义为具有 .second
组件。在这种情况下,即使您确实使用条件绑定来 "safely" 展开您的可选,在 nil
情况下您会做什么?
let theDate = Date()
let date = Date(timeIntervalSinceReferenceDate: 0) // "2001-01-01 00:00:00 +0000"
let seconds = Calendar.current.dateComponents([.second], from: date, to: theDate).second!
等同于直接调用:
let seconds = Int(theDate.timeIntervalSinceReferenceDate)