I would like to dry up a function for loading json arrays and Decodable models with one function, I am trying to pass an array pointer to the function to set the array within via a JSONDecoder.decode
the error I get is
error Cannot convert value of type '[T.Type]' to expected argument type 'T.Type'
I saw the thread Passing Decodable Object.Type as generic parameter
And perhaps its the solution, I used the part
where T : Decodable
from the thread but don't know enough beyond this. First app fully using Swift I haz no idea what im doing beyond trying to apply other languages GameDesign logic to Swift
func loadJSONAsEvent<T>(_ urlString: String, dataArrayEvents: inout [T.Type]) where T : Decodable {
guard let url = URL(string: urlString) else {
print("missing urrllll")
return
}
let session = URLSession.shared
let task = session.dataTask(with: url){(data,response, error) in
if(error == nil && data != nil){
let decoder = JSONDecoder()
do {
// this works
//let gg = try decoder.decode(SUBPLOTDATA.AllJSONEvents, from: data!)
// need to get this to work
// error Cannot convert value of type '[T.Type]' to expected argument type 'T.Type'
let ww = try decoder.decode(dataArrayEvents, from: data!)
//print(gg[2].featuredImage?.url as Any)
DispatchQueue.main.async {
SUPERAPP.shared.hasLoadedJSON = true
}
} catch {
//print("JSON error: \(error.localizedDescription)")
print(String(describing: error))
}
}
}
task.resume()
}
func loadJSONAsEvent<T>(_ urlString: String, dataArrayEvents: [T].Type) where T : Decodable {
guard let url = URL(string: urlString) else {
print("could not convert to URL: \(urlString)")
return
}
let task = URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data {
do {
let object = try JSONDecoder().decode(dataArrayEvents, from: data)
DispatchQueue.main.async {
SUPERAPP.shared.hasLoadedJSON = true
}
} catch {
print(error)
}
}
else if let error = error {
print(error)
}
}
task.resume()
}
Neat! thank you, that got the error out but it opened up other issues. Im not even sure I should be trying to do it this way. Since the new issues are a whole nother issue ill make a new post