2016-05-24 43 views
0

我遇到問題。NSURL錯誤 - 文件不存在

當我做print("Video URL: \(self.videoUrl.description)")我在輸出中得到這樣的:

Video URL: file:///var/mobile/Containers/Data/Application/92BDA035-131C-4FD2-A176-25F77C1D295B/Documents/video.mp4 

我用這來上傳文件到服務器:

func uploadVideo(){ 
     let currentUploads = PFObject(className: "myClassOne") 
     currentUploads.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in 
      if error == nil{ 
       //**Success saving, now save video.**// 

       currentUploads["userFile"] = try! PFFile(name: "video.mp4", contentsAtPath: self.videoUrl.description) 
       currentUploads.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in 
        if error == nil{ 
         // Take user home 
         print("Successfully uploaded") 
         }) 
        } 
        else{ 
         print(error, terminator: "") 
        } 
       }) 
      } 
      else{ 
       print(error, terminator: "") 
      } 
     }) 
    } 

我得到這個錯誤:

fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=4 "Failed to create PFFile at path 'file:///var/mobile/Containers/Data/Application/92BDA035-131C-4FD2-A176-25F77C1D295B/Documents/video.mp4': file does not exist." UserInfo={NSLocalizedDescription=Failed to create PFFile at path 'file:///var/mobile/Containers/Data/Application/92BDA035-131C-4FD2-A176-25F77C1D295B/Documents/video.mp4': file does not exist.}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-703.0.18.1/src/swift/stdlib/public/core/ErrorType.swift, line 54

有什麼不對?爲什麼我在成功獲取println的網址時得到「文件不存在」?任何想法/建議?

+0

嘗試'LS的/ var /移動/集裝箱/數據/應用/ 92BDA035-131C-4FD2-A176-25F77C1D295B /文檔/ video.mp4' – Jeef

+0

它的工作原理上模擬器? – Jeef

+0

@Jeef不工作的第一個鏈接,我不能在模擬器,因爲我使用手機相機拍攝視頻和上傳。 –

回答

0

使用self.videoUrl.path,而不是在你的try!表達self.videoUrl.description,如下:

currentUploads["userFile"] = try! PFFile(name: "video.mp4", contentsAtPath: self.videoUrl.path) 

videoUrl.description是URL的字符串表示,並有計劃"file://"之初,使其成爲一個無效的路徑在相比之下,videoUrl.path是與"/var/mobile/..."開始的字符串。)