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

逛奔的蝸牛

我不聰明,但我會很努力

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

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>
            亚洲一区在线直播| 国产一区av在线| 久久成人综合网| 久久久久久999| 亚洲激情国产精品| 亚洲欧美综合v| 亚洲国产精品一区二区尤物区| 嫩草成人www欧美| 国产真实乱偷精品视频免| 久久免费视频网站| 欧美一区三区二区在线观看| 久热这里只精品99re8久| 一区二区三区国产精华| 在线欧美日韩精品| 国产在线不卡视频| 国产日韩高清一区二区三区在线| 美国十次了思思久久精品导航| 亚洲欧美影院| 欧美在线看片a免费观看| 亚洲日本无吗高清不卡| 欧美性视频网站| 欧美电影免费观看| 欧美日韩亚洲91| 欧美另类女人| 亚洲自拍偷拍福利| 亚洲日本中文| 亚洲精品网址在线观看| 一区视频在线看| 91久久中文| 亚洲美女视频| 久久国产精品久久国产精品| 久久久精品视频成人| 亚洲国产精品传媒在线观看| 欧美激情欧美激情在线五月| 亚洲日本视频| 久久激情视频久久| 欧美日韩国产综合一区二区| 欧美性猛交xxxx乱大交蜜桃| 国产日韩欧美夫妻视频在线观看| 狠狠色噜噜狠狠狠狠色吗综合| 99热在这里有精品免费| 亚洲在线中文字幕| 久久久国产91| 亚洲尤物精选| 欧美日韩一区二区在线播放| 国产亚洲在线| 欧美亚洲一级| 欧美激情在线| 久久久久久亚洲精品杨幂换脸| 久久综合99re88久久爱| 一区二区三区导航| 欧美成人一区在线| 久久精品欧美日韩精品| 国产精品入口| 午夜日本精品| 亚洲欧美日韩综合国产aⅴ| 欧美日韩免费一区二区三区视频| 最新国产成人在线观看| 亚洲第一区在线观看| 美女日韩欧美| 日韩一级视频免费观看在线| 亚洲精品国产系列| 国产精品视频九色porn| 久久精品国产一区二区三区| 午夜日韩在线| 欧美精品一卡二卡| 亚洲电影免费观看高清完整版在线| 亚洲巨乳在线| 久久久蜜桃一区二区人| 亚洲欧美另类综合偷拍| 亚洲一区二区三区三| 国产精品日韩久久久久| 久久精品欧美日韩| 欧美激情视频在线播放| 99精品视频一区二区三区| 亚洲日本va午夜在线影院| 亚洲高清视频一区| 欧美黄色视屏| 日韩视频欧美视频| 欧美一区二区三区啪啪| 一本色道久久加勒比精品| 欧美一级在线亚洲天堂| 99re热精品| 免费日韩视频| 老鸭窝毛片一区二区三区| 国产精品swag| 99国产精品99久久久久久粉嫩| 国户精品久久久久久久久久久不卡| 欧美激情一区二区三区在线| 国产色爱av资源综合区| 国产精品99久久久久久久vr| 亚洲欧洲一区| 亚洲欧美久久久久一区二区三区| 99精品国产在热久久| 美女视频黄免费的久久| 久久久噜噜噜久久人人看| 国产片一区二区| 免费亚洲视频| 久久久天天操| 一区二区三区精品国产| 欧美大成色www永久网站婷| 亚洲视频一起| 久久久99精品免费观看不卡| 亚洲人成高清| 亚洲永久免费视频| 亚洲人屁股眼子交8| 亚洲一级在线| 最近中文字幕日韩精品 | 亚洲第一网站| 亚洲人成在线播放| 久久精品一区二区三区不卡| 亚洲一本视频| 欧美1区免费| 久久精品视频播放| 91久久精品美女| 国产精品日韩欧美一区| 亚洲一区二区三区久久 | 亚洲第一搞黄网站| 久久精品一区二区国产| 欧美成人一区二区| 老司机成人在线视频| 国产精品地址| 亚洲毛片在线观看| 亚洲国产欧美久久| 亚洲欧美国产va在线影院| 日韩视频在线观看| 久久久女女女女999久久| 亚洲视频导航| 欧美剧在线免费观看网站| 亚洲电影毛片| 国产日韩在线看| 午夜在线a亚洲v天堂网2018| 亚洲美女免费精品视频在线观看| 久久久久久久一区| 欧美在线观看视频一区二区| 欧美午夜精品久久久| 欧美激情小视频| 亚洲激情成人在线| 一区二区三区欧美日韩| 99在线|亚洲一区二区| 在线观看视频一区二区欧美日韩| 免费看成人av| 国产性猛交xxxx免费看久久| 在线亚洲一区观看| 夜夜嗨一区二区三区| 欧美日韩一区精品| 久久久国产91| 亚洲国产精品一区二区第一页 | 一区免费观看| 麻豆成人综合网| 久色婷婷小香蕉久久| 亚洲成人在线网| 久久久久久久久久码影片| 欧美成人午夜77777| 影音先锋在线一区| 欧美精品免费视频| 亚洲国产成人精品女人久久久 | 欧美高潮视频| 亚洲国产一区二区精品专区| 亚洲精品色婷婷福利天堂| 欧美国产免费| 亚洲私人影吧| 亚洲免费高清视频| 美脚丝袜一区二区三区在线观看| 久久精品国产亚洲一区二区| 韩国三级电影一区二区| 先锋影音一区二区三区| 男女精品网站| 亚洲人被黑人高潮完整版| 欧美日韩久久| 艳女tv在线观看国产一区| 欧美一区二区三区视频| 欧美福利网址| 午夜视黄欧洲亚洲| 亚洲福利视频网站| 亚洲素人一区二区| 一区二区三区在线免费视频| 久久久激情视频| 亚洲视频中文字幕| 亚洲免费大片| 韩国福利一区| 国产精品老牛| 久久久久久久久久久成人| 亚洲精品一二三| 欧美一区二区三区的| 亚洲精品美女久久7777777| 欧美日本韩国一区| 久久久噜噜噜久久久| 最新国产乱人伦偷精品免费网站| 欧美亚洲视频| 亚洲精品乱码久久久久久黑人| 欧美在线观看网址综合| 日韩一级片网址| 国产精品有限公司| 欧美三级不卡| 久久久一二三| 欧美在线观看一区二区| 亚洲第一主播视频| 欧美护士18xxxxhd| 亚洲精品色婷婷福利天堂|