2016-12-17 34 views
0

將pathArray作爲fileURLWithPath傳遞時出現錯誤。類型「URL」沒有類型「fileURL」 - 無法將字符串數組作爲字符串傳遞

func downloadImageForPhoto(_ photo: Photo, completionHandler: @escaping (_ success: Bool, _ errorString: String?) -> Void) { 

     taskForGETMethod(photo.photoURL, parameters: nil, parseJSON: false) { (result, error) in 
      if error != nil { 
       photo.imagePath = "unavailable" 
       completionHandler(false, "Unable to download Photo") 
      } else { 
       if let result = result { 
        DispatchQueue.main.async(execute: { 
         let fileName = (photo.photoURL as NSString).lastPathComponent 
         let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] 
         let pathArray = [path, fileName] 
         let fileURL = URL(fileURLWithPath: "\(pathArray)") 
         FileManager.default.createFile(atPath: fileURL.path, contents: result as? Data, attributes: nil) 

         photo.imagePath = fileURL.path 
         completionHandler(true, nil) 
        }) 
       } else { 
        completionHandler(false, "Unable to download Photo") 
       } 
      } 
     } 
    } 

回答

0

你不能指望通過使用字符串插值/分離器加入一個字符串數組。

你可以做的是

let pathURL = URL(fileURLWithPath: path) 
let fileURL = pathURL.appendingPathComponent(fileName) 

不過的確有到組件的數組傳遞給一個URL初始化的方法。雖然它與NSURL有關,但它返回URL實例。 PS:我總是建議使用URL相關的API來獲取Documents目錄並創建文件。

+0

謝謝你的幫忙先生:) –

相關問題