Skip to content

Instantly share code, notes, and snippets.

@jasonyunjoonpark
Created August 30, 2020 23:47
Show Gist options
  • Save jasonyunjoonpark/d458d4fe178e325e3b80e78396c5021b to your computer and use it in GitHub Desktop.
Save jasonyunjoonpark/d458d4fe178e325e3b80e78396c5021b to your computer and use it in GitHub Desktop.
Class that makes a get request to an API then parses the data into a Array<String> object but not sure how I return this object.
import Foundation
class AppUtils {
// Mark: Static class variables
static let requestBaseUrlString = "http://123.456.7.8:9999/"
// Mark: Get expirations for symbol
static func getExpirations(for symbol: String) {
// full get request url string
let requestUrlString = requestBaseUrlString + symbol
// submit get request
let request = URLSession.shared.dataTask(with: URL(string: requestUrlString)!) { (data, response, error) in
// make sure request returns data and has no errors
guard let data = data, error == nil else {
print("Get request for getExpirations for: '\(symbol)' returned an error.!")
return
}
// parse the get request data expirations
do {
let expirations = try JSONDecoder().decode([String].self, from: data)
print(expirations)
print(type(of: expirations))
//return expirations <----- i stupidly thought this would work...
} catch let jsonError {
print("Error serializing json data from get request!")
//return [] <----- the catch should return an empty Array
}
}; request.resume()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment