Here are two of the most common interview questions about KVC:

1. What is the assignment and value process of KVC? How does it work?

2. Does modifying attributes through KVC trigger KVO?

This article is around these two issues to elaborate, I believe you will have a deep understanding of these two issues after reading.

KVC review

Key-value coding (KVC) refers to iOS development that allows developers to access attributes of objects directly through Key names or assign values to attributes of objects. Without calling an explicit access method. This allows you to access and modify the properties of an object dynamically at run time. Not at compile time, which is one of the dark arts of iOS development. Many advanced iOS development techniques are implemented based on KVC.

Here are the four most important methods of KVC:

-(nullable id)valueForKey:(NSString*)key;

-(void)setValue:(nullable id)value forKey:(NSString*)key;

-(nullable id)valueForKeyPath:(NSString*)keyPath;

-(void)setValue:(nullable id)value forKeyPath:(NSString*)keyPath;

If you want to get the attributes of the current class, you use valueForKey and key. If you want to assign complex values such as the weight in cat, you have to use valueForKeyPath and keyPath to get and operate. Both of these are fairly simple, so just to recap. Let’s get down to business

1. What is the assignment and value process of KVC? How does it work?

We will use a diagram to illustrate how setValue:forKey: works.

First, we will look for setKey:,_setKey method, find the method directly assign value, can not find the next step;

We will write these two methods in GDPerson first to see if they are executed. (note: the.h method does not write attributes, because attributes generate set and get methods directly.) we will write these two methods in.m.)

NSUnknownKeyException = NSUnknownKeyException = NSUnknownKeyException = NSUnknownKeyException = NSUnknownKeyException

Second, the execution + accessInstanceVariablesDirectly (BOOL)

This method tells the program whether it has the key or not. The default is yes, so if we return no, the direct program will report an error. This is a little bit easier, so you can just copy the code and verify it, and if it returns yes, it goes to the next step.

+(BOOL)accessInstanceVariablesDirectly{

return NO;

}

_iskey, _iskey, key, iskey

Let’s write four member variables as follows:

{

@public

int_age;

int_isage;

int age;

int isage;

}

_age, _isage, age, isage(int_age; Remove it and try the second one, and so on.)

If none of the four are found, NSUnknownKeyException is reported and assigned

That’s how KVC assignment works, same thing -(void)setValue:(nullable id)value forKey:(NSString*)key; Assignment process is similar, you can try, use my method to try, this also know how to answer the interview question, the above picture is the answer, I believe you answer this, the interviewer should be very satisfied!

2. Does modifying attributes through KVC trigger KVO?

Take a look at the following code, we first use the code to verify the results, and then the process

From the screenshot above, it is easy to see that both forKey and forKeyPath trigger KVO

As we know from our last blog, directly changing the value of a member variable does not trigger KVO (like person.age=10), but why does KVC change?

And the reason I can tell you this is because when you assign KVC, it calls

Call -(void)willChangeValueForKey (NSString *)key, -(void)didChangeValueForKey (NSString) *)key can trigger KVO, so it is clear why KVC can trigger KVO

I’ll cover the basics of iOS categories in the next blog post.

If you find my writing helpful, please follow me and I will continue to update 😄