2017-05-30 59 views
0

我正在使用此代码为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") 
    } 
} 

什么是避免强制解包秒变量的正确方法?

+0

尝试使用,如果让语句设置之前,检查秒。 –

+0

在这种情况下,如果您完全确定'.second'在那里(因为**你把它放在那里!),你完全有理由强制解包。我只是将解开的力量移动到表达式的末尾。 – Alexander

+1

''com.myCompany.myApp“'字符串应该真的以编程方式获得...... – Alexander

回答

3
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) 
+2

由于问题是要求最好的方法来避免强制解包,所以这个答案是最好的,因为它完全避免了在使用更简单的代码时强制解包的需要。 – rmaddy

2

我想你在追逐一只红鲱鱼。你担心这一个力量解开,但你的整个代码块都包含在一个包含多个try s的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") 
    } 
} 

力解缠是不是普遍的“坏”。您使用force unwrap操作符是完全合理的,因为只是dateComponents定义为.second组件。在这种情况下,即使您使用条件绑定来“安全地”打开您的可选项,您将在nil的情况下做什么?

相关问题