青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

逛奔的蝸牛

我不聰明,但我會很努力

   ::  :: 新隨筆 ::  ::  :: 管理 ::
 

Cocoa and Objective-C: Up and Running (by me) is now available from O'Reilly.

Key-Value Coding (KVC) and Generic Programming

Key-Value Coding (KVC) is a Cocoa protocol for getting and setting values generically. In programming, the term "generically" describes a way of doing things that applies to many different situations.

Generic code can reduce to total amount of code in a project (which is always good) and helps software to handle situations that the programmer didn't anticipate. Generic, reusable code is emphasized throughout Cocoa.

For example, here's a non-generic way to set a first name and last name on an object:


[person setFirstName: @"Scott"];
[person setLastName:  @"Stevenson"];



This works fine, but I can use KVC messages to write more generic code:


[person setValue:@"Scott" forKey:@"firstName"];
[person setValue:@"Stevenson" forKey:@"lastName"];



Beginners might wonder what the point is here. In fact, it actually seems like the KVC version requires more typing. Let's choose another scenario where KVC's value is more apparent.

First, let's define the class:


@interface CDCPerson : NSObject
{
    NSString * firstName;
    NSString * lastName;
    NSNumber * phoneNumber;
    NSString * emailAddress;
}
- (void) setFirstName:    (NSString *)value;
- (void) setLastName:     (NSString *)value;
- (void) setPhoneNumber:  (NSNumber *)value;
- (void) setEmailAddress: (NSString *)value;
@end



Now, some actual code:


// assume inputValues contains values we want to
// set on the person

NSDictionary * inputValues;
CDCPerson    * person = [[CDCPerson alloc] init];
                                  
NSEnumerator *e = [inputValues keyEnumerator];
id dictKey, dictValue;

while ( dictKey = [e nextObject] )

    dictValue = [inputValues valueForKey: dictKey];
    [person setValue: dictValue forKey: dictKey];
}



This snippet of code is generic, meaning that we don't need to change it everytime new instance variables are added to the Person class.

But it gets better! Here's an even simpler version of the code above:


// assume inputValues contains values we want to
// set on the person

NSDictionary * inputValues;
CDCPerson    * person = [[CDCPerson alloc] init];

[person setValuesForKeysWithDictionary: inputValues];



Intrigued? Here's Apple explanation of what's happening in -setValuesForKeysWithDictionary:


Sets properties of the receiver with values from keyedValues, using its keys to identify the properties. The default implementation invokes setValue:forKey: for each key-value pair, substituting nil for NSNull values in keyedValues.


In other words, essentially the same as the first example. But what is -setValue:forKey: actually doing? This is where the KVC magic comes in. It will actually find the -setFirstName:, -setLastName:, -setPhoneNumber: and -setEmailAddress: implementations and call those. If it can't find these, KVC will try quite a few different options before ultimately just setting a value on the instance variable itself.

KVC can also be used to pull values out of an object:


// assume person already exists and is populated with values

CDCPerson * person;

NSMutableDictionary * outputValues;
outputValues = [NSMutableDictionary dictionary];

NSArray * keys;
keys = [NSArray arrayWithObjects: @"firstName",
                                  @"lastName",
                                  @"phoneNumber",
                                  @"emailAddress",
                                  nil];

NSEnumerator *e = [keys objectEnumator];
id key, value;

while ( key = [e nextObject] )

    value = [person valueForKey: key];
    [outputValues setValue: value forKey: key];
}




Or, the simpler version:


// assume person already exists and is populated with values

CDCPerson    * person;
NSArray      * keys;

keys = [NSArray arrayWithObjects: @"firstName",
                                  @"lastName",
                                  @"phoneNumber",
                                  @"emailAddress",
                                  nil];

NSDictionary * outputValues;
outputValues = [person dictionaryWithValuesForKeys: keys];



Just as with setting values, getting values with -valueForKey: will cause KVC to look for a method the same name as the key:


// this will cause KVC to look for a method called -firstName;

NSString * name = [person valueForKey:@"firstName"];



Key-value coding is key element in Cocoa Bindings and Core Data, so it really pays to understand the basic ideas. KVC can handle keypaths, such as:


// getting
[obj valueForKeyPath: @"storage.firstName"];

// setting
[obj setValue: @"Scott" forKeyPath: @"storage.firstName"];



This is similar to doing:


// getting
[[obj storage] firstName];

// setting
[[obj storage] setFirstName:@"Scott"];



For more details on Key-value coding, take a look at this page on ADC.
Design Element
Key-Value Coding (KVC) and Generic Programming
Posted Oct 03, 2005 — 11 comments below




 

SteveJ — Oct 03, 05 403

In two places where you send the CDCPerson class the allocate message, you don't need the "]" after the class "CDCPerson" and before the message "allocate".

Scott Stevenson — Oct 03, 05 404 Scotty the Leopard

Fixed, thanks. I think this was partially due to a smart editing rule in the text editor.

Daniel Jalkut — Oct 03, 05 405

Just a vote of confidence for this "experiment." I think you have a great writing style and the formatting of your articles is very accessible. The approach you're taking now could produce a sigfnicant "FAQ-like" resource for newbies and oldbies alike.

Samo Korosec — Oct 04, 05 406

Aren't such articles what Cocoa Dev Central is about anyway? Will you repost them there, too?

Jesper — Oct 04, 05 407

Samo: Scott uses the CDC prefix for one-off classes in the article, so it seems like the goal is to get it there later on, but to publish it here now so that people can read it in the meantime.

Scott Stevenson — Oct 04, 05 408 Scotty the Leopard

Jesper's is basically on the right track. It's easier to experiment quickly here on the blog, and move stuff to CDC once it's cleaned up a bit.

Samo Korosec — Oct 04, 05 415

Ah, okies. I've just read over the CDC* stuff not thinking about it. Great idea to post such Cocoa helplets, though.

Tito Ciuro — Mar 31, 06 997

Thanks for the article! I'd like to see keypaths explained a little better. For example, if CDCPerson had an NSArray *siblings (with a few CDCPerson), how could we obtain the list the siblings?

I'd think we would call it via [person valueForKeyPath: @"siblings.firstName"], but I'm not sure if I'd need extra accessors, especially if we start dealing with a fairly nested dictionary. How would that work?

Thanks again!

Scott Stevenson — Apr 03, 06 1000 Scotty the Leopard

I'd think we would call it via [person valueForKeyPath: @"siblings.firstName"]

That should work fine.

hisham — Dec 29, 08 6577

Great. I'm new to KVC and this really helped :)

Thanks so much,
Hisham

Daniel — Mar 20, 09 6626

"Beginners might wonder what the point is here. In fact, it actually seems like the KVC version requires more typing. Let's choose another scenario where KVC's value is more apparent."

That's exactly why I was googling for more info. :) Thanks! Good info.




 

Comments Temporarily Disabled

I had to temporarily disable comments due to spam. I'll re-enable them soon.




Technorati Profile
Copyright © Scott Stevenson 2004-2008

From: http://theocacao.com/document.page/161

@import url(http://m.shnenglu.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
posted on 2011-12-02 00:05 逛奔的蝸牛 閱讀(359) 評論(0)  編輯 收藏 引用 所屬分類: Cocoa
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            一片黄亚洲嫩模| 久久精品官网| 一区二区日韩伦理片| 欧美激情亚洲激情| 久久久蜜桃精品| 久久精品道一区二区三区| 欧美一区二区三区在线视频| 亚洲欧美www| 亚洲女人天堂成人av在线| 亚洲一区视频在线观看视频| 一区二区三区欧美| 亚洲女人小视频在线观看| 欧美在线视频免费| 久久亚洲精品网站| 亚洲国产91精品在线观看| 亚洲国产毛片完整版 | 日韩视频一区二区在线观看 | 日韩系列欧美系列| 日韩一级二级三级| 一本久久综合| 亚洲欧美另类久久久精品2019| 亚洲午夜激情| 久久成人久久爱| 欧美国产视频在线观看| 亚洲美洲欧洲综合国产一区| 夜夜嗨av一区二区三区网页| 亚洲免费在线| 欧美成人a∨高清免费观看| 欧美日韩精品一区二区天天拍小说 | 亚洲一区二区久久| 久久激情五月激情| 欧美国产另类| 国产精品久久久久av| 国内精品国产成人| 亚洲高清久久| 午夜精品在线看| 欧美国产精品v| 亚洲男女自偷自拍图片另类| 久久久久久久久岛国免费| 欧美日韩午夜在线视频| 国产一区久久久| 在线视频日韩精品| 亚洲视频1区| 国产欧美日韩另类视频免费观看| 美国十次了思思久久精品导航| 欧美精品久久一区二区| 在线亚洲一区观看| 午夜精品久久| 欧美成人在线影院| 黑人操亚洲美女惩罚| 亚洲精品午夜精品| 久久国产精品久久国产精品| 亚洲国产精品一区二区www| 亚洲一二三级电影| 欧美日韩高清一区| 亚洲国产成人久久| 久久精品久久综合| 中文日韩在线| 欧美日韩免费观看一区二区三区| 在线精品视频一区二区三四| 欧美在线亚洲一区| 夜夜嗨av一区二区三区 | 欧美日韩在线播放一区| 亚洲欧美日本另类| 老司机精品导航| 久久综合亚洲社区| 亚洲国产精品第一区二区三区| 久久精品免费| 亚洲福利免费| 亚洲毛片视频| 久久狠狠一本精品综合网| 久久理论片午夜琪琪电影网| 91久久久久久国产精品| 免费的成人av| 亚洲一级一区| 久久综合色婷婷| 国产农村妇女毛片精品久久莱园子 | 香蕉久久精品日日躁夜夜躁| 国产精品盗摄久久久| 99天天综合性| 一级日韩一区在线观看| 国产精品久久久久久久午夜片| 亚洲男人影院| 99热在这里有精品免费| 99国产精品视频免费观看一公开| 欧美理论片在线观看| 亚洲一区国产精品| 欧美亚洲尤物久久| 在线欧美一区| 亚洲欧洲精品一区二区三区不卡| 欧美久久久久久久久久| 亚洲美女中文字幕| 亚洲国产精品久久91精品| 久久久国产午夜精品| 国产视频观看一区| 亚洲欧美综合国产精品一区| 一区二区三区高清视频在线观看| 欧美激情精品久久久久久蜜臀| 亚洲精品久久久久久下一站 | 亚洲日本成人| 国产精品免费看片| 久久人人97超碰精品888| 亚洲盗摄视频| 午夜免费在线观看精品视频| 国内精品久久久久影院优| 欧美成人一二三| 欧美午夜精彩| 开心色5月久久精品| 欧美日韩三级在线| 久久蜜臀精品av| 欧美成人精品在线观看| 午夜精品视频一区| 免费欧美日韩| 性欧美暴力猛交69hd| 欧美精品1区2区3区| 欧美自拍丝袜亚洲| 欧美巨乳波霸| 欧美高清免费| 国内精品国产成人| 一区二区三区回区在观看免费视频| 伊人春色精品| 亚洲私拍自拍| 亚洲视频日本| 欧美日韩三级电影在线| 免费在线成人av| 国语精品中文字幕| 亚洲一区二区三区乱码aⅴ蜜桃女 亚洲一区二区三区乱码aⅴ | 欧美精品一区二区三区在线播放| 老色批av在线精品| 国产亚洲精品高潮| 亚洲欧美日韩中文视频| 亚洲欧美欧美一区二区三区| 欧美国产日韩在线| 欧美国产日韩视频| 亚洲第一精品福利| 欧美专区在线观看| 久久精品亚洲| 国产一区二区三区日韩| 午夜精品区一区二区三| 欧美资源在线观看| 国产手机视频精品| 午夜免费日韩视频| 国产精品久久久久久久第一福利| 女仆av观看一区| 国产女人18毛片水18精品| 亚洲少妇在线| 欧美日韩在线高清| 欧美成人在线网站| 亚洲精品美女久久久久| 1000部国产精品成人观看 | 久久综合久久综合这里只有精品| 欧美在线看片| 一区二区高清在线观看| 欧美怡红院视频一区二区三区| 亚洲精品免费观看| 亚洲小说春色综合另类电影| 国产精品久久99| 亚洲一区二区精品视频| 国产主播一区二区| 欧美福利在线观看| 99精品热视频只有精品10| 欧美影院视频| 亚洲国产精品嫩草影院| 亚洲高清久久久| 亚洲大胆在线| 日韩图片一区| 免费亚洲一区二区| 亚洲美女啪啪| 欧美在线播放一区| 99精品视频一区| 国产亚洲一本大道中文在线| 欧美一区二区视频在线| 黄色资源网久久资源365| 欧美精品尤物在线| 久久九九久精品国产免费直播| 亚洲国产高清在线观看视频| 久久噜噜亚洲综合| 久久嫩草精品久久久精品| 一本大道久久a久久综合婷婷 | 亚洲国产精品一区二区三区| 亚洲伊人一本大道中文字幕| 久久成人免费| 午夜精品一区二区三区四区 | 久久免费高清视频| 99视频+国产日韩欧美| 亚洲国产成人91精品| 久久精品av麻豆的观看方式| 午夜精品久久久久久99热| 亚洲国产成人精品女人久久久| 久久久精品一区| 蜜臀av国产精品久久久久| 国内在线观看一区二区三区 | 一区二区三区成人| 免费亚洲一区| 久久久精品国产免大香伊| 一区二区日韩免费看| 亚洲国产精品久久久久秋霞影院| 久久九九热re6这里有精品| 欧美精品亚洲二区| 亚洲欧美春色|