2012-04-06 200 views
16

我正在做一些數據寫入我的文檔目錄,我想知道是否有辦法告訴我的writeToFile方法何時完成,並且我正在創建的文件已完全創建。在此先感謝這裏是我打電話給我的方法,我只是在尋找一種方法,它是一種委託方法或其他方式來告訴何時完成。writeToFile如何知道它何時完成

[imageData writeToFile:fullPathToFile atomically:YES]; 

尼克

回答

31

方法writeToFile:atomically爲 「同步」。它將寫入文件,然後根據文件是否成功寫入或返回YESNO返回。

這意味着只要方法返回,操作就完成了。

BOOL success = [imageData writeToFile:fullPathToFile atomically:YES]; 
// Now, the operation is complete 
// success indicates whether the file was successfully written or not 
+1

如果保存不成功,有沒有辦法獲取錯誤日誌? – aneuryzm 2014-03-20 11:34:09

+3

@Patrick你可以使用方法[writeToFile:options:error:](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html#//apple_ref/OCC/instm/NSData的/將writeToFile:選項:錯誤:) – sch 2014-04-03 15:15:32