Using runtime to implement the Set,get method of dynamically changing the properties in model class

Source: Internet
Author: User

Yesterday I probably elaborated on the concept of the framework control container, but if you want to plump the idea there are a lot of questions, such as if I want to manage some controller is bound to involve controller loading and caching, And the premise to do this is to build a controller table. Manages the string that is set in the table to manage the corresponding controller. The traditional way to create a table has many, the most commonly used is the plist file, but for iOS developers, if the plist write dead, in the management of the time will be more rigid, to know that OC is a flexible, dynamic language. If we use nsuserdefaults, it is not very convenient to observe. So what I'm going to share today is that the Set,get method of using the class to dynamically change the attributes in the model class.

Why use a class item? Because the class is more convenient to use, we do not have to specifically restrict which attribute, and portability is relatively high. and using runtime, we no longer have to be sick of a change set and get method, we only need a little code to be able to rewrite the internship method. Well, gossip less, on the code.


We first create an object that points to itself, and then copy all of its properties.

Next we start to iterate through all the attributes and rewrite his set,get method. Just a mouth. Imp, this thing should be very unfamiliar to the new iOS development, in fact it is a function pointer, in the API document is so defined its typedef ID (*IMP) (ID, SEL), and IMP can be obtained from the object & SEL method. If you want to know more about IMP, you can take a look at the message mechanism and I won't say much here. How did this setter,getter come true?

We see that both functions have two parameters, one is the ID type of obj, the other is the SEL type of the _cmdme, in fact, the parameter names of these two parameters can be casually written, which I write, mainly for the convenience of understanding. This is the dynamic addition of the Set,get method, if we want to have more operations, can be in both methods to write.

Finally, let's use the category we wrote. First we randomly create a model, and of course we must inherit nsobejct.

The model name I created here is called Viewcontrollername, and of course I think it's more appropriate to call Viewcontrollernamelist, obviously I built it for the cache list, and we'd better pay attention to the naming conventions when we write the project. That's when it should call the set method we just wrote.

After we run the print sandbox file discovery, it already exists in our local plist file. This way we can see through the model that we manage the controllers altogether and prepare for the subsequent cache list. and using the Get method is the same as what we usually use, here I do not do more introduction. If there is any problem, can feedback to me in time, thank you!

Using runtime to implement the Set,get method of dynamically changing the properties in model class

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.