0

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)
                   
                }
            }
Mansidak
  • 136
  • 6
  • `let inputparameters = inputParameters(A1: ""...` -> `let inputparameters = inputParameters`? But class/struct names should start with an uppercase. – Larme Jan 16 '23 at 16:20
  • @Larme I tried that. But when I do that it says Parameters2 doesn't conform to 'Encodable' anymore :/ – Mansidak Jan 16 '23 at 16:26

1 Answers1

0

Your code doesn't work for several reasons:

  • A key mismatch inputParameters (dictionary key) vs. inputparameters (struct member).
  • The mentioned error which occurs because you assigned a default value in 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

vadian
  • 274,689
  • 30
  • 353
  • 361
  • I see. Interesting. So when you ask to encode 'params' do you mean to define another struct foe 'params'? – Mansidak Jan 16 '23 at 16:34
  • No. Just `let data = try JSONEncoder().encode(params)` – vadian Jan 16 '23 at 16:35
  • Gotchu. However my functoin requires the input to conform to encodable as follows: func execute(parameters: Parameters2) and I previously had Parameters2 conform to encodable. How would I manage that if I use JSONEncoder(). Am I able to explain what I'm trying to say? :/ Sorry I'm still learning these topics. – Mansidak Jan 16 '23 at 16:39
  • Because even when I use JSONEncoder I get 'Cannot convert value of type 'Data' to expected argument type 'Parameters2'' – Mansidak Jan 16 '23 at 16:42
  • Please edit the question and add the code how you encode the object. – vadian Jan 16 '23 at 16:43
  • Just updated the question with the function that takes in 'params' – Mansidak Jan 16 '23 at 16:46
  • 1
    Replace `parameters: Parameters2` with `parameters: [String: InputParameters]` – vadian Jan 16 '23 at 16:49
  • Okay, that eliminates the error form the function but when calling it by using JSONEncode I still have the error 'Cannot convert value of type 'Data' to expected argument type '[String : InputParameters]'' near '.encode(params)' – Mansidak Jan 16 '23 at 16:55
  • cuz when I use 'try' next to JSONEnocder, my button that executes the function throws error 'Invalid conversion from throwing function of type '() throws -> Void' to non-throwing function type '() -> Void'' – Mansidak Jan 16 '23 at 16:56
  • No, AF encodes the parameters implicitly. Call the method `executePost(parameters: params)` – vadian Jan 16 '23 at 16:58
  • Yes, that works. Thanks, man! Appreciate the help. Now I'm back to my trouble of parsing Json and hopefully, I can figure that out lol. – Mansidak Jan 16 '23 at 17:24
  • Decode the stuff with `responseDecodable`. – vadian Jan 16 '23 at 17:51
  • Holy cow how I did miss that in the docs. Real quick: would my Response type be 'String' or 'encodable'? I'm kinda confused on the docs response type references. Regardless, thanks for the suggestion! – Mansidak Jan 17 '23 at 01:37
  • Hmmm interesting. Looks like I need to create a struct for the kind of data I'm expecting in the response and them set that as the Decodable type. – Mansidak Jan 17 '23 at 01:49
  • Hey @vadian, I was able to get the struct and everythingworking however everytime I decode it, it returns nil. For some reason my jsonData is being shown as ' XXbytes' when I parse it as Data. Do you think you'd be able to help me privately with this? – Mansidak Jan 17 '23 at 05:28
  • Please ask a new question and show the relevant information. – vadian Jan 17 '23 at 05:33
  • I asked it here: https://stackoverflow.com/questions/75142639/jsondecoder-returning-nil-while-parsing – Mansidak Jan 17 '23 at 06:23
  • Why do you ask two almost identical questions? I suggested a solution in your other question. – vadian Jan 17 '23 at 07:53