Recently in the study of Swift, I used to write C #, then turned to OC, so I often complained about why OC is not generic. Now Swift is out, and it supports generics, but I can't help but have to spit out swift.
The thing is, the data we do is basically obtained from the server, and the data returned by the server is basically returned in JSON format, then the JSON data is first converted to a dictionary or an array by the client. Some projects are directly using the converted Josn dictionary array, and most of the projects should be further encapsulated, converting the JSON data into corresponding class instances, which I call a DTO (data to object). So how do you convert a converted dictionary or array of JSON data into an instance of a corresponding class? The reflection technique is used here, the first is to dynamically generate an object instance based on the name of the class, and then assign a value to each property of the class, and the process of this assignment has two ways, one is to use the Setvaluesforkeyswithdictionary assignment directly, The other is to use reflection to reflect the property list of a class, and then use KVC to assign values, and I use reflection technology in my project.
The above-mentioned DTO process is also the same as that of swift, but the method of reflection is different, but the concrete principle is the same. And where is the problem I'm having? is on the generic type. Let me give you an example to see the code below
class classgeneric<t> { var property:t?}
This can be written in Swift, and this code is similar in C #. Why do you write this, I would like to use a generic class of friends to this kind of writing is no stranger, but such a generic class in Swift but does not support KVC, if you use KVC to set the property, the runtime will report "This class is notkey value Coding-compliant". I broke the idea that I wanted to make a dto out of generics.
Another thing I want to spit out is that Swift's reflection is too weak to do too little.
Swift Learning Notes