[KVC supplements Objective-C language]
Mobile
2023-07-29 19:10:28
views: null
1. KVC Supplement
Ok, then, let me tell you about this KVC
1. First of all, let's say, what does this KVC refer to?
key value coding
Bar
The full name is called (Key Value Coding), which is its full name
Then, when you search in the help document, you search for key-value coding
Is it this, key-value coding
then click to enter
These are some basic introductions about KVC
2. Then we introduce some commonly used methods in this KVC
First, create a new project
name: demonstrate the common methods of KVC
Oh, we are an iOS program, there is no need to use an iOS program
Let's use a Mac program, it's OK
Ok, let's first create a new class for you
Create a new Dog class
Is this a dog?
Person in a moment, Person in a moment
Does the dog have a name, does it have a name, uh, breed
Ok, let’s build another person, the Person class
Do people have a dog?
Well, do people have a name, a name?
This is a person's name,
Do people have a dog?
Import the header file for the "Dog" class
Well, there's a dog inside
So, next, watch out,
Inside the main.m file
here
create a person object
Create a dog object
This person's name is "Zhang San"
And then, another dog
what is it called, chihuahua
Then, let our p1.dog be equal to this chihuahua
p1.dog = chihuahua;
Of course I have to let the dog have a name
chihuahua.name = @"Chihuahua";
Okay, then, next, after we have assigned the value here, of course, there is no need to ask the following questions,
I will print it directly below
NSLog(@“%@—%@“,p1.name,p1.dog.name);
One must be Zhang San and the other is a Chihuahua
Yes or no
Isn't this man named Zhang San, and he has a dog named Chihuahua
Well, this is our normal usage
Ok, then, next, I will show you the common usages here.
The first thing we demonstrate to you is to create a class and assign values to these objects through our ordinary way of writing code
Next, demonstrate a way, through KVC,
Assign values to object properties
setValuesForKeys
this way
For example, we give everyone, come back,
1) First create a new dictionary like this
above is
// directly assign values to the properties of the object
Below is
//Assign a value to the object through kvc
For example, I wish to setValuesForKeys for this p1
We can use this setValue forKeyPath
first and second
1)forKey:
2)forKeyPath
basically the same
but forKeyPath
more powerful than the first
how strong
talk later
Do we want to now, let's call this method one by one and assign it a value, forKeyPath
Call this method, assign values to its properties, keyPath
p1 setValue:(id) forKeyPath:(NSString *)
1) Then, what is the value of the first parameter, (id), called @“李四”
Do you want to assign a @”李四” to it?
Then, which attribute to assign a value to?
2) The second parameter, forKeyPath: (NSString *)
Which attribute of p1 is assigned @"李四"?
@“name”
Assign a @"李四" to the name attribute of p1
Well, let's have another age
what is it called, assign
@property(nonatomic,assign)int age;
OK, give it another one, give it an age
p1 setValue:(id) forKeyPath:(NSString *)
age, note
In our object, we can't directly save our ordinary data, right?
give it something
Come @10, in this case, is it converted into an object type?
Origin blog.csdn.net/madoca/article/details/131934327