我擴展了一個簡單的導入/導出功能的應用程序,它使用plist文件來存儲導出的對象。要保存對象,我使用NSDictionary
和writeToURL
方法(沙盒應用程序)。如何從NSDictionary writeToURL獲取詳細的錯誤消息?
代碼的相關部分看起來是這樣的:
savePanel.beginSheetModalForWindow(self.view.window!) { (result) in
savePanel.orderOut(nil)
if result == NSFileHandlingPanelOKButton {
let saveURL = savePanel.URL!
let values = NSMutableDictionary()
for propertyDescription in selectedEvent.entity.properties {
let propertyName = propertyDescription.name
values[propertyName] = selectedEvent.valueForKey(propertyName)
}
if !values.writeToURL(saveURL, atomically: true) {
// error handling?
}
}
}
現在我想知道如果有一種方式來獲得詳細的錯誤信息如果保存文件失敗?
'writeToFile'僅返回一個布爾https://developer.apple.com/library/watchos/documentation/Cocoa/Reference/Foundation/類/ NSDictionary_Class/index.html#// apple_ref/occ/instm/NSDictionary/writeToFile:原子地: – Moritz
@EricD我完全意識到這一點。因此,我的問題:有沒有辦法如何獲得詳細的錯誤消息,如果此方法失敗? – Flovdis
可以使用NSData的方法'FUNC writeToURL(網址:NSURL,選擇writeOptionsMask:NSDataWritingOptions)throws'如果你想處理錯誤 –