iOS Development Series-ui Foundation-KVC

Source: Internet
Author: User

This knowledge is UI primary learning, currently I am still learning, suitable for beginners to see

Kvc-key value Coding that is, key-value encoding is a way to get values and set values

When we create a class file that sets the member properties for this class:

Create a "person" class: a name (name), age two public member attributes:

Because the @property keyword is used, the setter (set value), getter (get Value) method is generated directly in HFPERSON.M, which is the corresponding point syntax. Because two member properties are public, we can access them anywhere, such as I do in main.m, create a Human object, assign a value to the Name property

So now that you have setter and Getter methods, why do you have KVC to get the value and set the value?

Here are some of the more powerful KVC than ordinary setter and getter methods.

I add an extension in the hfperson.m file, which is the private property of human sex (sex) attribute,

At this point we look at the breakpoint with a sex attribute in the Human object

Here I set the Sex property for the P1 object in main.m to see what happens:

There is no way to access the private properties of this class in another class, so now I want to implement it in MAIN.M or set the Sex property.

Here's the first powerful place for KVC:

In KVC's eyes, without a private attribute, any property can be accessed, then we use KVC to set the sex property of the P1 object in main.m, and get the sex attribute

At this point we pass the KVC to P1 's sex private property, set the value, also obtains the value, this is the setter getter cannot do,

This method name:-(void) SetValue: (nullable ID) value Forkey: (NSString *) key;

Here's how:-(void) SetValue: (nullable ID) value Forkeypath: (NSString *) KeyPath; This method can implement the function of the above method, so what is the difference from the above method?

Now there is a new requirement: Create a "book" Class with a name (bookname) private attribute, at which point I add an object property to the human just created

Next, I create the book object in the MAIN.M, assigning a value to the P1 object's Books property

Now there is a new requirement, how to set or get the value of book's private Property (BookName) through P1? The normal point syntax is definitely not going to work.

Then I use-(void) SetValue: (nullable ID) value Forkey: (NSString *) key;

Book.bookname is not a property member of a P1 object. Book is a property member of the P1 object, and BookName is the property member of the book. So how do you change bookname directly by P1?

Use-(void) SetValue: (nullable ID) value Forkeypath: (NSString *) KeyPath; method

Introduce this method, [P1 setvalue:@ "Nine Yin Canon" forkeypath:@ "Book.bookname"]; Use this method, it will think. The preceding is an object, which is a member property of the P1 object. The following is the member property of this object. This is the unique function of this method;

Next, let's take a look at the second powerful feature:

First introduce these several functions, now learn these several, later have new discovery, will update in time.

The first time to write a blog, I hope you can read.

iOS Development Series-ui Foundation-KVC

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.