0

I was thinking of using a plist file to configure how I would upload a form to my server but then I realised I don't know how I could do one crucial part of this or even if this is possible since you can't dynamically declare variables with swift.

Basically I was thinking of using a dictionary like this

form:

  • property_name: backend_name

and then in my form upload method I would do something that would look like this

formValues: [String:String] = [:]
form.forEach { (property_name, backend_name) in
  let value = someController[property_name] // to do the equivalent of someController.property_name
  formValues[backend_name] = value
}
// ...
formValues.forEach { (backend_name, value) in
  multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: backend_name)
}

the problem is I don't know how to this part

let value = someController[property_name]
James Woodrow
  • 365
  • 1
  • 14

2 Answers2

3

The problem you're trying to solve is serializing your data for a server. I don't know whether the approach you're trying is possible in Swift, but it's definitely not the right one. If you're using Swift 4, you could create Codable model objects, so this serialization will be all under the hood. Otherwise, you'll most likely need to create this [String: Any] manually.

Using Codable to serialize/deserialize objects

Connor Neville
  • 7,291
  • 4
  • 28
  • 44
  • So you're saying that you can't access a class property dynamically ? it would probably be for the same reason you can't create a class property dynamically either Edit: I'm using swift 3.2 – James Woodrow Mar 10 '18 at 13:29
  • I'm not sure whether you could possibly achieve what you want with `KeyPath`s. They also got a revamp in Swift 4 so they're more powerful. Another thing to possibly look into. – Connor Neville Mar 10 '18 at 13:32
  • But yeah, in summary, Swift has a strong type system and likes knowing the types of everything. Can make problems like this difficult. – Connor Neville Mar 10 '18 at 13:32
  • I took some time to mark the answer, sorry, completely forgot and although I found how to do it how I imagined it I chose to go with serializing my data – James Woodrow Sep 22 '18 at 09:11
0

I found how I could do it.

formValues: [String:String] = [:]
form.forEach { (property_name, backend_name) in
  let value = Mirror(reflecting: someController).children.filter({ (child) -> Bool in
    return child.label == property_name
}).first!.value as! String
  formValues[backend_name] = value
}
// ...
formValues.forEach { (backend_name, value) in
  multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: backend_name)
}

I know this is unoptimised since I recreate a mirror each cycle but it's for demonstration purposes

James Woodrow
  • 365
  • 1
  • 14