[KVC supplements Objective-C language]

1. KVC Supplement

notify
notify

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

notify

Then, when you search in the help document, you search for key-value coding

notify

Is it this, key-value coding

then click to enter

notify

These are some basic introductions about KVC

notify

2. Then we introduce some commonly used methods in this KVC

First, create a new project

notify

name: demonstrate the common methods of KVC

notify

Oh, we are an iOS program, there is no need to use an iOS program

notify

Let's use a Mac program, it's OK

notify

Ok, let's first create a new class for you

notify

Create a new Dog class

notify

Is this a dog?

notify

Person in a moment, Person in a moment

Does the dog have a name, does it have a name, uh, breed

notify

Ok, let’s build another person, the Person class

notify
notify

Do people have a dog?

Well, do people have a name, a name?

notify

This is a person's name,

Do people have a dog?

notify

Import the header file for the "Dog" class

Well, there's a dog inside

So, next, watch out,

Inside the main.m file

notify

here

notify

create a person object

Create a dog object

notify

This person's name is "Zhang San"

notify

And then, another dog

what is it called, chihuahua

notify

Then, let our p1.dog be equal to this chihuahua

p1.dog = chihuahua;

notify

Of course I have to let the dog have a name

notify

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

notify

Yes or no

notify

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.

notify

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

notify

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

notify

above is

// directly assign values ​​to the properties of the object

Below is

//Assign a value to the object through kvc

notify

For example, I wish to setValuesForKeys for this p1

notify

We can use this setValue forKeyPath

notify

first and second

1)forKey:

notify

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

notify

Call this method, assign values ​​to its properties, keyPath

p1 setValue:(id) forKeyPath:(NSString *)

notify

1) Then, what is the value of the first parameter, (id), called @“李四”

notify

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”

notify

Assign a @"李四" to the name attribute of p1

notify

Well, let's have another age

notify

what is it called, assign

@property(nonatomic,assign)int age;

notify

OK, give it another one, give it an age

notify

p1 setValue:(id) forKeyPath:(NSString *)

age, note

notify

In our object, we can't directly save our ordinary data, right?

give it something

notify

Come @10, in this case, is it converted into an object type?

Guess you like

Origin blog.csdn.net/madoca/article/details/131934327