2016-06-08 76 views
1

我想在開始時創建預種子數據庫 該文件相當大(5mb)。iOS - Swift如何知道copyItemAtPath何時完成?

我用copyItemAtPath來複制文件,那麼這個方法有沒有完成? 我如何知道這個過程已經完成?

+2

當copyItemAtPath方法返回,它不是異步複製完成。如何通過拋出錯誤來報告失敗? –

回答

1

這段代碼就足夠了:

do { 
    // copy files from main bundle to documents directory 
    print("copy") 
    try 
     NSFileManager.defaultManager().copyItemAtPath(sourcePath, toPath: destinationPath) 
} catch let error as NSError { 
    // Catch fires here, with an NSError being thrown 
    print("error occurred, here are the details:\n \(error)") 
} 

其中的DestinationPath可以是例如:

NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, .UserDomainMask, true).first