2017-01-15 60 views
1

我目前正在製作一個應用程序,將數據發送到處理它的算法算法。然後在何處找到文件響應被髮送回應用程序以這種形式:從算法響應中提取數據

Optional({ 
output = "data://.algo/deeplearning/AlgorithmName/temp/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.png";}) 

我需要一種方法來提取的隨機產生的「XXXXXXXX-XXXXXXXX-XXXX-xxxxxxxxxxxx.png」部響應。它需要存儲在一個我稍後會用到的字符串中。

+1

只需從您的url /路徑中獲取lastPathComponent。 'URL(string:「data://.algo/deeplearning/AlgorithmName/temp/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.png」)?lastPathComponent? 「」' –

+0

@LeoDabus我不完全確定如何做到這一點。之前的代碼行表示'let response = resp.getJson()'然後'print(response)'基本上說得到來自Algorithmia API的響應。然後在調試器中打印響應(如上所述)。 – Ben

+1

我相信你在尋找'response?['output']爲?字符串「來提取該URL字符串。那麼Leo的評論應該能夠爲你提供文件名。 – anowell

回答

1

最終代碼:

if let json = response as? [String: Any] { 
       print(json) 

       let filePath = json["output"] as? String 
       print("File path: \(filePath!)") 

       let uwFilePath = filePath! 

       let index = uwFilePath.index(uwFilePath.startIndex, offsetBy: 57) 
       self.imageFileName = uwFilePath.substring(from: index) 

       print(self.imageFileName) 
      } 

映像文件名稱被存儲最終的文件名稱供以後使用。 輸出字符串的開始部分也被切斷。