Understanding of the Category property of iOS

Source: Internet
Author: User

In Objective-c, you can add attributes to an existing class by category, but you cannot add an instance variable

Anyway read the first time I was a little dizzy, you can add "properties", and then say "add instance variable", the first feeling seems a bit contradictory. So let's talk about:

    • What is an instance variable?

An instance variable is a variable that can hold data. On OC, the form is as follows:

@interface myviewcontroller:uiviewcontroller{    *MyButton;} @end
    • What is a property?

A property is a new mechanism for OC and requires that you declare an instance variable corresponding to it, which is the earliest concept, as the compiler converts from GCC to LLVM (Low level virtual machine), it is no longer necessary to declare an instance variable for a property. If LLVM discovers a property that does not have a matching instance variable, it automatically creates an instance variable that begins with the underscore.

    • Category can add attributes to an existing class

I set up a uicolor hex classification, and on the uicolor+hex.h I did something like this:

Then do you will find that the compiler will not report any errors, build it will not be a problem, of course, this is not to say that this code can be run "crash". If you are careful, you will notice that there is a warning in. m.

It says that the Sethex and hex methods of attribute hex need to be defined using @dynamic or implemented by themselves. The attributes added here do not automatically generate instance variables, and the attributes added here are actually added getter and setter methods.

Here, we can not understand the above that sentence, in fact, is equivalent to this statement

Only methods can be added to the category, and instance variables cannot be added

First, we use the @dynamic to do:

The warning did disappear immediately, but could it be run? The dream is beautiful, the result is still crash.

    • So what did the @dynamic do to make the warning clear, and of course you will find out where this @dynamic has met? By the way, when you use CoreData, you will find "Nsmanagedobject yourself to explore".

@dynamic are relative to the @synthesize, they are used to modify the @property to generate the corresponding getter and setter methods. But @ dynamic means that the getter and setter methods of this member variable are not generated directly by the compiler, but are generated either manually or at run time.

@dynamic just tells the compiler that the getter and setter methods is implemented not by the class itself but somewhere Else (like the superclass or would be a provided at runtime).

Then why did it crash? Haha, don't take it for granted, because you didn't provide getter and setter methods in runtime. Of course it will collapse.

Above, we basically understand some of the problems of adding properties to the category. So how to solve these problems, is also the following I would like to say associated Objects, let ' s go!

First of all, we carry 2 questions:

    1. Where is the associated object stored in the memory of the associated object itself?
    2. What is the life cycle of the associated object? When is it released and when is it removed?

The above is the implementation of two methods

    • Objc_setassociatedobject
    • Objc_getassociatedobject
    • Objc_removeassociatedobjects

Write at night again

Understanding of the Category property of iOS

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.