1

I am currently making an app that sends data to an Algorithmia algorithm where it is processed. A response of where to find the file is then sent back to the app in this form:

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

I need a way to extract the randomly generated 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.png' section of the response. It needs to be stored in a string which I will use later.

Community
  • 1
  • 1
benfernandes
  • 179
  • 3
  • 13
  • 1
    just get the lastPathComponent from your url / path. `URL(string: "data://.algo/deeplearning/AlgorithmName/temp/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.png")?.lastPathComponent ?? ""` – Leo Dabus Jan 15 '17 at 01:09
  • @LeoDabus I'm not entirely sure how to do that. The lines of code before says `let response = resp.getJson()` then `print(response)` basically saying get the response from the Algorithmia API. It then prints the response (stated above) in the debugger. – benfernandes Jan 15 '17 at 02:02
  • 1
    I believe you're looking for `response?['output'] as? String` to extract that URL string. Then Leo's comment should be able to get you the filename. – anowell Jan 15 '17 at 22:46
  • It worked after a little tweaking with how the json code was initially entered into the `['output']` bit.
    I ended up writing changing the response into a string before the output was taken. The output also had to be written like `["output"]` instead. Thank you for the help!
    – benfernandes Jan 16 '17 at 11:50

1 Answers1

1

Final code:

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)
            }

imageFileName is storing the final file name for later use. The begining part of the output string is also being cut off.

benfernandes
  • 179
  • 3
  • 13