Swift Learning Notes

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.