Skip to main content

What is Key-Value Coding (KVC) and Why We Need It?

Before I discuss what is Key-Value Coding (KVC), it's very important that you understand the concept of KeyKey-Value and Key-Path.

What is Key?

Lets say we have a class named "Person" and it has 2 ivars called "name" and "age". We will call these ivars as "keys" but they will be strings => our ivar called "name" will have the key called @"name" and "age" ivar will have the key called @"age".

What is Key-Value?

Since ivar is suppose to store some value/data => our keys will have a value/data and we will call it key-value.

And What is Key-Path?

Lets say we have 2 classes as follows.

  @interface Passport {  	NSString *passportId; } @end
  @interface Person {  	NSString  *name; 	NSInteger *age;  	Passport  *passport; }  @end

Now what will be key for Passport class ivar called "passportId"? Answer is, it will depend on the perpective. If we will look it from the Person class, its key will be @"passport.passportId" and if we look it from the Passport class itself, it will be@"passportId".

So this "Path tracing" of key is called "Key-Path"

What is Key-Value Coding (KVC)?

In object oriented (OO) programming, you will also make getters and setters to set and access the values of your ivars, which is very good thing from the perspective of Encapsulation. But KVC is the concept of setting and getting the values of ivars with keys instead of getter and setters like this.

  Person *person = [[Person alloc] init];  // To set value of ivar with keys [person setValue:@"Adil" forKey:@"name"];  // To get the value of name ivar with keys [person valueForKey:@"name"];

Why We Need This?

The obvious question which arise from this, if we can do the same thing with getters and setters, why go through this hassle? The simple answer is, it reduce the amount of code we write but how? Just compare the following 2 codes for the following senario.

  /* Lets say we need the value associated with our ivars at run time but we don't know in advance  which ivar need to be returned. */  // Code 1 - without KVC  - (id*) returnIvarValueFor:(NSString*) key {  	if ([key isEqualToString:@"name"]) { 		return [self name]; 	} else if ([key isEqualToString:@"age"]) { 		return [self age]; 	} }  // code 2 - with KVC  - (id*) returnIvarValueFor:(NSString*) key {  	return [self valueForKey:key]; }

Code 2 is obviously simpler and easy to understand.

Popular posts from this blog

Nested Collection Models in MVC to Add Multiple Phone Numbers - Part 3

This is Part 3 of the article series. Actually, in this article series we were developing an MVC application that will allow adding multiple phone numbers using Nested Model concept. User will be able to add or remove (zero or more phone numbers, I need minimum two phone numbers for each employee) phone numbers for any single employee. Previous logs: Nested Collection Models in MVC to Add
✔ Read More...
I guess you came to this post by searching similar kind of issues in any of the search engine and hope that this resolved your problem. If you find this tips useful, just drop a line below and share the link to others and who knows they might find it useful too.

Stay tuned to my blogtwitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address t…

iPhone Application for Islamic Duas

Islamic Duas is an app designed for Muslims around the world who don’t get the time to go through Islamic books and are looking for authentic duas to pray. This app is a solution to all your needs as every Muslim knows that a prayer in time can save you from a lot of trouble. This app carries important Islamic duas for different occasions, daily life events and end of prayer duas to seek forgiveness from Allah etc. These Islamic duas are collected from authentic sources and are very beneficial in life of a Muslim. The app is available for free for iOS and Android users. Use the following buttons to download the ap. 

Download from here:

Muslim Some of the Islamic duas included in Muslim Dua Now are: Duas for dressing/undressing/new clothesDuas for entering and leaving restroomDuas Before and After AblutionDuas for hearing AdhanDuas for Entering and leaving mosqueDua against distraction from ShaytanDuas for protection from evilDuas before and after eatingDuas to recite when fearing harm fro…

HTML5 and CSS3 Zebra Striping - Look Ma No JavaScript

It was 5 maybe 6 years ago when I first started learning jQuery. One of the first things I did was order the jQuery In Action book. If you have read that book you should remember one of the first examples given, zebra striping a table. To me this example sold me on the idea of jQuery even if I did not actually understand how jQuery worked at the time. With one line of code I could now alternate background colors in a table.
✔ Read More...
I guess you came to this post by searching similar kind of issues in any of the search engine and hope that this resolved your problem. If you find this tips useful, just drop a line below and share the link to others and who knows they might find it useful too.

Stay tuned to my blogtwitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. W…