2017-01-15 66 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) 
      } 

映像文件名称被存储最终的文件名称供以后使用。 输出字符串的开始部分也被切断。