0

I understand how to make REST requests and handle the returning JSON data, but now I need to work with an API that returns data as plain text, one word per line.

Assuming I can just make a standard GET request, how do I parse the plain text response into something I can use—an array of strings?

Any help greatly appreciated.

cmilr
  • 379
  • 4
  • 13

1 Answers1

1

Usually you receive the string as UTF8 encoded Data.

Create the string from the data and separate the paragraphs by the newlines CharacterSet which considers many different newline separators.

let paragraphs = String(data: data, encoding: .utf8)!.components(separatedBy: .newlines)
vadian
  • 274,689
  • 30
  • 353
  • 361