Featured post
objective c - What is the difference between valueforKey:, objectForKey:, and valueForKeyPath:? -
i have 2 questions:
what difference between
valueforkey:
,objectforkey:
? 1nsdictionary
s (objectforkey:
) , othersvalueforkey:
, or reverse?also difference between
valueforkey:
,valueforkeypath:
? has got core data?
please help.
valueforkey:
part of nskeyvaluecoding protocol , therefore part of key-value coding framework, allows access class properties name @ runtime. that's how nibs loaded, example — names of properties associated connections loaded , values set directly name. contrasts way visual interface design tools work in other languages, generating lots of hidden statically compiled code.
objectforkey:
defined on dictionaries only, , looks object key. nsdictionary class stores connections between values , keys.
so, valueforkey:
used on nsdictionary return meta information dictionary, such count of objects inside it, list of keys, etc. objectforkey:
used dictionary.
at runtime, difference objectforkey:
method opaque implementation. valueforkey:
explicitly relies on subsequently calling named getters , setters. reason latter can extend key-value coding key-value observing, ask informed every time particular property on particular object changes. @ runtime that's achieved method swizzle, original setter replaced new 1 calls previous setter , sends out required messages. because messages dispatched dynamically, that's achieved modifying tables within object instance.
so object key-value coding compliant (which means declaring , implementing properties in proper way, new-ish @property/@synthesize syntax automatically) can observed without object having implement code.
there's further apple stuff uses key-value coding achieve various things, including coredata, it's not enable 1 other technology.
valueforkeypath:
valueforkey:
except can traverse several objects. can have root object bunch of properties, each of properties object bunch of properties, etc, , using key path can retrieve value way out @ leaf of data structure rather having iterate through object after object yourself.
in summary, valueforkey:
, valueforkeypath:
provide information object instances , interact dynamic nature of objective-c runtime. objectforkey:
dictionary specific method dictionary tasks.
additions:
an example, coded type , assuming nsdictionary key-value coding compliant:
nsdictionary *somedictionary; // create somedictionary, populate it, example (note: assume photoofkeys.jpg // exists, not idea production code — if doesn't we'll // nil there , after won't added dictionary it'll appear // terminated list): somedictionary = @{ @"favouritegarment": @"hat", @"@allkeys" : [nsimage imagenamed:nsimagenamedotmac], @(2) : nsarray.new }; nsobject *allkeys; // make no assumptions type @allkeys be, going assume // can nslog it, needs descendant of nsobject rather 'id' // respond 'description' message — compile // time semantics, if else reads code it'll make obvious them // thinking... // code of keys stored in dictionary , print them out; // should print array containing strings 'favouritegarment', '@allkeys' , // number 2 allkeys = [somedictionary valueforkey:@"@allkeys"]; nslog(@"%@", allkeys); // code object named '@allkeys' dictionary; print // description of image created loading photoofkeys.jpg, above allkeys = [somedictionary objectforkey:@"@allkeys"]; nslog(@"%@", allkeys); // `objectforkey analogous `objectforkeyedsubscript:`, aka allkeys = somedictionary[@"@allkeys"];
allkeys
property of nsdictionary described here. i've added mapping nsstring allkeys photograph of keys. whether use key-value coding valueforkey:
methods or nsdictionary objectforkey:
lookup method dictates whether read property of object instance or whether send object instance message asking unique job.
- Get link
- X
- Other Apps
Comments
Post a Comment