2

Is it possible in Swift to create a struct that has "dynamic" properties?

i.e. if I have a dictionary that is returned from some JSON API and I know at build time that it has name, id in it.

Then I can build a struct like...

struct MyStruct {
    let name: String
    let id: String
}

But then if the dictionary has a custom attribute like age... can I do something to add a typed property to the struct at run time?

I know (think) this sort of thing was possible with Objective-C but there was a lot more runtime control.

Is it possible in Swift?

Thanks

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • 2
    For people Googling: this question is _not_ about the Swift [`dynamic` modifier](https://docs.swift.org/swift-book/ReferenceManual/Declarations.html#ID381) keyword. This question is about creating new properties at runtime (like with [`objc_setAssociatedObject(_:_:_:_:)`](https://developer.apple.com/documentation/objectivec/1418509-objc_setassociatedobject) in Objective-C objects). – Cœur Jul 01 '19 at 03:27
  • Related: [How to dynamically add properties to an existing class then access them](https://stackoverflow.com/questions/54240593/). The answer uses a dictionary to store the dynamic key-values. – Cœur Jul 01 '19 at 03:28

1 Answers1

-1

Simply add a variable to your class that is optional, as follows:

var age: Int?

Hope that helps

Florensvb
  • 209
  • 1
  • 10
  • 1
    Yeah, but I can't use `myObject.age` and get a typed output. If that's not possible I'll fall back to using raw dictionaries and not have a struct at all :D – Fogmeister Feb 15 '17 at 16:20
  • sorry i meant to store the age as an optional, i have edited my answer – Florensvb Feb 15 '17 at 16:23
  • But only if I know at compile time that there is something called `age`. I don't necessarily know what types and names of attributes are going to exist at the time of writing the code. (If that makes sense). – Fogmeister Feb 15 '17 at 16:25
  • Yes, because your variable age is now declared as an optional, you are always gonna have to unwrap the value whenever you are using it, using the '?' parameter – Florensvb Feb 15 '17 at 17:23
  • Yeah, but I don't know what it might be called at compile time. It will change after the code is distributed. It might be `age` it might be another attribute called `blahBlahSomethingCustom` or some other name. The point is, I don't know what the custom attributes will be or will be called at the time of writing the code. – Fogmeister Feb 15 '17 at 17:24
  • Either you need to exactly define what you are getting in your API call, so that you know what to expect. If you're not sure what type of parameters you can expect at all, then a custom struct is not a good choice, because you in fact can not know the names and types of your objects. Rather try to iterate over your dictionary using for (key,value) in dictionary {} in order to do something with the values inside. – Florensvb Feb 15 '17 at 17:27
  • Yeah, that was my alternative. With Objective-C you can access the runtime environment and add properties and types at runtime. Which would mean I could add any attribute to the JSON and the object would have it as a property without any code change. Thanks for the help :D – Fogmeister Feb 15 '17 at 17:28