2015-12-04 41 views
0

我試圖在Swift中使用NSFileManager的方法createDirectoryAtPath:withIntermediateDirectories:attributes:error:使用Swift創建文件夾

問題是,我不知道這個功能是什麼throws在出現錯誤的情況下。這是否記錄在任何地方?如果是,在哪裏?

+0

您需要處理的錯誤執行會盡力追趕 –

+0

BTW https://developer.apple.com/library/搶鮮/ IOS /文檔/雨燕/概念/ Swift_Programming_Language/ErrorHandling.html –

回答

2

當Swift文檔中提到一個函數throws時,它們表示拋出一個NSError

考慮以下do-try-catch流量:

let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] 

do { 
    try NSFileManager.defaultManager().createDirectoryAtPath(documentsPath, withIntermediateDirectories: false, attributes: nil) 
} catch { 
    print(error) 
    print(error.dynamicType) 
} 

createDirectoryAtPath將失敗,因爲文件的目錄已經存在。記錄的errordynamicType表明,它實際上是一個NSError對象:

Error Domain=NSCocoaErrorDomain Code=516 "The file 「Documents」 couldn’t be saved in the folder 「35B0B3BF-D502-4BA0-A991-D07568AB87C6」 because a file with the same name already exists." UserInfo={NSFilePath=/Users/jal/Library/Developer/CoreSimulator/Devices/E8A35774-C9B7-42F0-93F1-8103FBBC7118/data/Containers/Data/Application/35B0B3BF-D502-4BA0-A991-D07568AB87C6/Documents, NSUnderlyingError=0x7fa88bd14410 {Error Domain=NSPOSIXErrorDomain Code=17 "File exists"}} 

NSError