Hi all so I need to call a function that takes in the following parameters:
let params = [
"inputParameters" : ["A2":"a1","B1":"b1","C1":"c1","D1":"d1"]
]
so I made the following encodable struct for it
struct Parameters2 : Encodable{
let inputparameters = inputParameters(A1: "", B1: "", C1: "", D1: "")
}
struct inputParameters: Encodable {
let A1: String
let B1: String
let C1: String
let D1: String
}
However when I call
guard let params = Parameters2(inputParameters(A1:"something", B1:"something", C1:"something", D1:"something"))
else{
return
}
I get an error 'Argument passed to call that takes no arguments' even though I clearly defined the input arguments in the struct. Can anyone figure out what could be going wrong?
EDIT: Upon @vadian's suggestions I'm adding the function where this parameter is plugged into:
func executePost(parameters: Parameters2, completion: @escaping (Any?, Error?) -> Void) {
AF.request("myURL",
method: .post,
parameters: parameters,
encoder: JSONParameterEncoder.default, headers: headers).response { response in
debug print(response)
if let error = response.error {
completion(nil, error)
}
else if let jsonArray = response.value as? Any{
completion(jsonArray, nil)
}
else if let jsonDict = response.value as? Any{
completion([jsonDict], nil )
}
}}
And I call the function as follows:
executePost(parameters: JSONEncoder().encode(params)) { (json, error) in <-- error Cannot convert value of type 'Data' to expected argument type '[String : InputParameters]'
if let error = error{
print(error.localizedDescription)
}
else if let json = json {
print(json)
}
}
Your code doesn't work for several reasons:
inputParameters
(dictionary key) vs. inputparameters
(struct member).Parameters2
.guard let
expects an optional, but there is none.An easier way is one struct
struct InputParameters: Encodable {
let A1, B1, C1, D1: String
}
and a literal dictionary key
let params = ["inputParameters": InputParameters(A1:"something", B1:"something", C1:"something", D1:"something")]
and encode params