First, let’s recall a classic interview question

Can a Category add member variables? If so, how do I add member variables to a Category? So the first thing we know from previous knowledge is that the Category is generated at the bottom as follows

As we can see from the above structure, there is no property definition, so it can’t be added directly, what is it? OK, let’s continue exploring.

Let’s explore this step by step: first I create a GDPerson that inherits NSObject, and then I create a GDPerson+Test category.

As follows:

We know that the age in GDPerson is the system generates three variables for us: member variables; The get method; The set method, and the Test class declare a height attribute. Let’s look at the program in action:

It just crashed, because the height of the class Test was just declared for us, and it didn’t actually implement it, so let’s do this

We assigned 20, and it didn’t work, so we thought, well, there must be something wrong with the implementation in the category. According to the corresponding scenario, we can think of the following schemes to try:

Scenario 1: Set global variable assignment

The solution is as follows:

GDPerson*person = [[GDPersonalloc]init];

person.age=10;

person.height=20;

NSLog(@”age:%d,height:%d”,person.age,person.height);

Output :age:10,height:20, There are drawbacks, such as the following

Solution 2: Set the global dictionary scheme storage

Because of the one-to-one correspondence, we came up with the following solution, and we knew in the last blog that we could initialize it by saying load, because we wanted it to be unique, so I took the address of the current object as the key.

Height = 20; height = 20; height = 20; That sounds like a solution, but if we add one more variable, we’ll have to define an NSMutableDictionary, and we’ll have to write a lot of code. You may also find some other problems, such as memory leaks (depending on how you look at the problem), because you are holding the NSMutableDictionary. Of course, there is also the problem of thread safety, maybe we need to add some knowledge of locking, multithreading, etc. This scheme is ok, but it is very troublesome.

Which brings us to today’s main subject: correlation objects

Set const void *key to the associated object

First take a look at some definitions of the runtime associated object we used

The associated object is a method provided by Apple, which is an object associated property. Many of the internal operations are handled by Apple official, so we can use it safely. As for the specific variable, for example, objc_AssociationPolicy(the figure below), you can click on the definition to see how it is very clear Const void *key = const void *key = const void *key

Since it’s a const void star key, LET me just define a const void star variable and try it out, so here’s the code

Hey, that seems to solve the problem, the height is really 20, is that right? If you’re careful, you might notice that there’s a downside to this, because your GDKey is not unique, because we defined it this way, which means that this value is null. If you add one more variable, it’s not unique.

Scheme 4: Set the constant area key of the associated object

Const void *key = const void *key = const void *key = const void *key = const void *key The string is also an object, so if you write it this way, it has a constant area, and its pointer address is unique everywhere, so this is definitely going to work. Here’s the code

Height is 20, there is no problem, this scheme is also a better scheme, the only small drawback is @”height” may appear by mistake, so be careful.

Set @selector(key) for associated objects (recommended)

This is not an error-prone method, and I recommend it:

The core objects involved in setting the associated object are as follows

Among them HashMap is more common in Java, we can think of it as our dictionary, is a key-value pair, after as long as you see a map or HashMap can be thought of as a key-value pair, specific source code you can own research.

Conclusion: The above is the solution of several cases I analyzed. Of course, there are other schemes for classifying and adding attribute variables. I think using associated attributes is the best scheme, and it is also the simplest to write.

If you have a better plan, please let me know at 😄. ————————————————

Included: Original address