I have a Singleton class:
class Session {
static let sharedInstance = Session()
private init() {}
// app properties
var token: String!
var promotions: JSON!
}
In a networking class I have multiple async calls that I fire off using a map
function for each url:
let urls = [ "promotions": "http:..." ]
let results = urls.map { (k, url) in
self.getFromApi(url, params: ["token" : token]) { response in
guard response as? JSON != nil else { return }
Session.sharedInstance[k] = response
// the above should compile to Session.sharedInstance["promotions"] = response.
}
The issue lies in trying to set the response on the sharedInstance by subscript. I've attempted to implement a subscript on the Singleton to no avail:
subscript(property: Any) -> Any {
get {
return Session.sharedInstance[property]
}
set(newValue) {
Session.sharedInstance[property] = newValue
}
}
I've also attempted to forego subscripting via conformance to NSObject
and using KVC
however this also didn't work & furthermore I lose all type safety.
Any help or advice is very much appreciated. Thanks in advance.