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

逛奔的蝸牛

我不聰明,但我會很努力

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

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://www.shnenglu.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
posted on 2011-12-02 00:05 逛奔的蝸牛 閱讀(365) 評論(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| 久久久免费av| 国产精品永久免费在线| 一本一本久久a久久精品综合妖精 一本一本久久a久久精品综合麻豆 | 久久久无码精品亚洲日韩按摩| 欧美激情aⅴ一区二区三区| 欧美国产日韩精品| 狠狠久久亚洲欧美| 亚洲欧美日韩成人| 久久婷婷久久| 狠狠色伊人亚洲综合网站色| 欧美激情视频一区二区三区免费 | 先锋影音久久久| 欧美日韩视频一区二区| 亚洲电影一级黄| 一本色道久久综合亚洲精品婷婷 | 国产精品亚洲综合| 亚洲性夜色噜噜噜7777| 亚洲福利视频免费观看| 亚洲一区二区日本| 久久国产手机看片| 亚洲视频axxx| 日韩一区二区精品视频| 欧美—级a级欧美特级ar全黄| 亚洲欧美日韩精品久久久| 久久婷婷久久| 久久精品亚洲乱码伦伦中文| 毛片av中文字幕一区二区| 亚洲欧美激情在线视频| 久久成人免费| 亚久久调教视频| 亚洲精品老司机| 久热爱精品视频线路一| 伊人久久成人| 欧美成人精品在线| 欧美不卡视频一区| 99爱精品视频| 在线亚洲电影| 国产精品入口麻豆原神| 亚洲三级免费观看| 亚洲第一狼人社区| 欧美在线不卡| 亚洲人体影院| 99这里只有精品| 国产欧美日韩中文字幕在线| 亚洲美女网站| 日韩亚洲欧美精品| 欧美成人在线网站| 亚洲国产专区校园欧美| 国产精品白丝av嫩草影院 | 久久久久国内| 久久丁香综合五月国产三级网站| 欧美色一级片| 久久性色av| 欧美午夜国产| 亚洲天堂激情| 欧美一区二区视频在线观看2020 | 午夜亚洲一区| 久久亚洲精品欧美| 国模精品娜娜一二三区| 亚洲国产综合视频在线观看| 亚洲电影在线看| 正在播放欧美视频| 欧美一区二区在线视频| 国产精品一香蕉国产线看观看 | 在线视频免费在线观看一区二区| av成人天堂| 国产精品久久久久高潮| 亚洲福利视频网| 日韩视频在线免费| 欧美日韩情趣电影| 校园春色综合网| 老色鬼精品视频在线观看播放| 在线成人av.com| 欧美激情一区二区三区在线视频| 亚洲久久成人| 久久精品国产一区二区三区免费看 | 中文精品一区二区三区| 午夜免费久久久久| 欧美日韩国产在线看| 欧美成在线视频| 黄色工厂这里只有精品| 蜜桃av一区二区| 99精品热视频| 久久综合狠狠综合久久综合88| 亚洲激情影院| 国产精品视频最多的网站| 欧美一区二区三区精品| 亚洲国产另类久久精品| 亚洲欧美中文另类| 亚洲国产成人精品久久久国产成人一区 | 久久另类ts人妖一区二区| 亚洲欧洲日产国产综合网| 国产精品私拍pans大尺度在线 | 久久久av网站| 久久蜜桃精品| 亚洲中无吗在线| 国产精品久久婷婷六月丁香| 亚洲精品一区二区三区蜜桃久| 午夜在线精品| 99这里只有精品| 国语自产精品视频在线看8查询8 | 亚洲自拍另类| 久久精品国产99国产精品澳门| 亚洲国产欧美一区二区三区同亚洲 | 欧美午夜精品久久久久久孕妇| 久久狠狠婷婷| 亚洲资源在线观看| 一区二区三区精品视频| 欧美国产日韩亚洲一区| 久久精品五月| 欧美一区二区三区日韩视频| 99精品国产高清一区二区| 在线观看av一区| 国产一区二区三区不卡在线观看| 欧美在线日韩在线| 亚洲一区二区3| 一本久久a久久免费精品不卡| 你懂的国产精品| 久久噜噜噜精品国产亚洲综合| 午夜亚洲福利在线老司机| 亚洲视频1区| 中日韩男男gay无套| 一本色道婷婷久久欧美| 亚洲精品乱码久久久久久蜜桃麻豆| 欧美激情bt| 欧美激情亚洲国产| 欧美大片免费| 欧美成人精品h版在线观看| 久久人人97超碰精品888| 久久精品亚洲国产奇米99| 香蕉久久一区二区不卡无毒影院 | 欧美~级网站不卡| 夜夜嗨av一区二区三区网页| 亚洲韩国青草视频| 91久久一区二区| 久久久精彩视频| 妖精成人www高清在线观看| 亚洲欧洲另类| av不卡在线看| 亚洲女人天堂成人av在线| 亚洲一区影音先锋| 欧美在线观看网站| 久久久视频精品| 欧美激情精品久久久久久久变态| 欧美大片在线观看一区| 亚洲全黄一级网站| 中文一区在线| 欧美一区二区在线| 麻豆成人小视频| 欧美日韩国产va另类| 国产精品成人播放| 国产偷久久久精品专区| 激情成人av在线| 国产精品入口麻豆原神| 激情欧美亚洲| 日韩一级免费| 久久精品国产欧美亚洲人人爽| 久久一区视频| 亚洲片国产一区一级在线观看| 中文国产成人精品久久一| 久久精品视频网| 欧美精彩视频一区二区三区| 国产精品国码视频| 亚洲第一天堂无码专区| 国产精品99久久久久久有的能看 | 亚洲欧洲日韩女同| 午夜精品久久久久久久蜜桃app| 久久国产精品99国产| 欧美激情在线播放| aa日韩免费精品视频一| 久久久久国产精品一区| 欧美午夜电影在线| 亚洲国产毛片完整版 | 亚洲欧美资源在线| 欧美电影电视剧在线观看| 亚洲一二三区精品| 欧美极品在线观看| 国产在线一区二区三区四区| 一区二区久久久久久| 美女性感视频久久久| 亚洲在线播放电影|