2017-07-02 146 views
-1

do中的語句未被執行。任何人都可以幫助我解決這個問題?非常感謝!無法將「String」類型的值轉換爲預期的參數類型「URL」

class func copyFile(fileName: NSString){ 

    let fileManager = FileManager.default 

    let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask) 

    guard documentsURL.count != 0 else{ 
     return 
    } 

    let finalDatabaseURL = documentsURL.first!.appendingPathComponent(fileName as String) 

    if !((try? finalDatabaseURL.checkResourceIsReachable()) ?? false){ 
     print("Database does not exist in documents") 

     let documentsURL = Bundle.main.resourceURL?.appendingPathComponent(fileName as String) 

     do{ 
      try fileManager.copyItem(at: (documentsURL?.path)!, to: finalDatabaseURL.path) 

     }catch let error as NSError{ 
      print("Couldn't copy file to final location!Error:\(error.description)") 
     } 
    }else{ 
     print("Database file found at path:\(finalDatabaseURL.path)") 
    } 


} 
+3

的[無法將類型的價值可能重複'字符串?到期望的參數類型'URL'](https://stackoverflow.com/questions/40360716/cannot-convert-value-of-type-string-to-expected-argument-type-url) – Parker

回答

0

copyItem(at srcURL: URL, to dstURL: URL)需要兩個URL參數,並且您正在傳遞字符串。

你可以使用這個。

try fileManager.copyItem(at: documentsURL!, to: finalDatabaseURL) 

try fileManager.copyItem(atPath: (documentsURL?.path)!, toPath: finalDatabaseURL.path) 
相關問題