2017-05-30 63 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的情況下做什麼?

相關問題