<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>mikeash.com pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html comments</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#comments</link><description>mikeash.com Recent Comments</description><lastBuildDate>Sun, 10 May 2026 05:35:00 GMT</lastBuildDate><generator>PyRSS2Gen-1.0.0</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Ivan - 2015-09-30 23:09:59</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#comments</link><description>Hi Mike, thank you for your great articles, carry on writing please!
&lt;br /&gt;
&lt;br /&gt;I've found a mistake in the article. Actually, the property searching algorithm looking at underscored ivars before then non-underscored ones. Moreover, it checks ivars started with &lt;code&gt;is&lt;/code&gt;, thus the complete search path is: setter/getter -&amp;gt; &lt;code&gt;_&amp;lt;Key&amp;gt; -&amp;gt; _is&amp;lt;Key&amp;gt; -&amp;gt; &amp;lt;Key&amp;gt; -&amp;gt; is&amp;lt;Key&amp;gt;&lt;/code&gt;.
&lt;br /&gt;
&lt;br /&gt;Actually there is many of other interesting things about searching algorithms in KVC, all of them are describet here &lt;a href="http://apple.co/1KSAh1t"&gt;http://apple.co/1KSAh1t&lt;/a&gt;</description><guid isPermaLink="true">ef84382a1f0e5ec2e9831d29031cb992</guid><pubDate>Wed, 30 Sep 2015 23:09:59 GMT</pubDate></item><item><title>Recovery - 2014-10-20 14:01:03</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#comments</link><description>What's the best way to pass values to different video controllers, KVC, Delegate or something else?</description><guid isPermaLink="true">78b0ff841c27a699a1d91c4c0def2b72</guid><pubDate>Mon, 20 Oct 2014 14:01:03 GMT</pubDate></item><item><title>Morty Eraser - 2014-10-16 15:33:55</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#comments</link><description>This is indeed an amazing tutorial on explaining one of the most important concepts in iOS programming. Looking forward to more posts like this one in future.</description><guid isPermaLink="true">3f2c487d43c9baeaf3f1dec75e71870e</guid><pubDate>Thu, 16 Oct 2014 15:33:55 GMT</pubDate></item><item><title>Carick - 2014-10-13 16:34:10</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#comments</link><description>I found KVC is great for passing values between UIViewControllers, especially for segues. It's much simple than Delegate, which i am still confused although learnt the concept a few months ago. 
&lt;br /&gt;
&lt;br /&gt;BTW, really great tutorial. I need to spend some time to fully digest the content.</description><guid isPermaLink="true">3ca2a88f7adc6a30f4724e08ef083bfa</guid><pubDate>Mon, 13 Oct 2014 16:34:10 GMT</pubDate></item><item><title>Manoj - 2013-03-25 10:04:29</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#comments</link><description>Great article !! 
&lt;br /&gt;
&lt;br /&gt;Always impressed the way you put down things in simple but in precise.
&lt;br /&gt;</description><guid isPermaLink="true">a520724e1b84573823b53c88c1d17fbd</guid><pubDate>Mon, 25 Mar 2013 10:04:29 GMT</pubDate></item><item><title>George - 2013-02-16 15:31:24</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#comments</link><description>Mike, i'm wondering... did you work at Apple, or actually have a friend inside?. I'm amazed by the simplicity of your implementations... and i was just being curious about... if you wrote the actual thing!
&lt;br /&gt;</description><guid isPermaLink="true">3b416f7e5e22ec4e443e9f5c3c4b20f4</guid><pubDate>Sat, 16 Feb 2013 15:31:24 GMT</pubDate></item><item><title>Matthias Plappert - 2013-02-13 06:55:16</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#comments</link><description>Great article. One thing that seems to be missing are @properties with a custom getter, e.g.
&lt;br /&gt;&lt;div class="blogcommentquote"&gt;&lt;div class="blogcommentquoteinner"&gt;@propert (assign, getter=isEnabled) BOOL enabled;&lt;/div&gt;&lt;/div&gt;
&lt;br /&gt;Any insight how does work with KVC?</description><guid isPermaLink="true">8ae0284e3eec96f55224ece16b0bf099</guid><pubDate>Wed, 13 Feb 2013 06:55:16 GMT</pubDate></item><item><title>Justin Spahr-Summers - 2013-02-10 07:38:28</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#comments</link><description>Great article!
&lt;br /&gt;
&lt;br /&gt;It's worth noting that KVC will only access ivars if +accessInstanceVariablesDirectly isn't overridden to return NO: &lt;a href="https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Protocols/NSKeyValueCoding_Protocol/Reference/Reference.html#//apple_ref/occ/clm/NSObject/accessInstanceVariablesDirectly"&gt;https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Protocols/NSKeyValueCoding_Protocol/Reference/Reference.html#//apple_ref/occ/clm/NSObject/accessInstanceVariablesDirectly&lt;/a&gt;
&lt;br /&gt;
&lt;br /&gt;The behavior of nil when using KVC to set a primitive value is also somewhat interesting.</description><guid isPermaLink="true">0ad1be7403f2d53faeba83dd992ac6c9</guid><pubDate>Sun, 10 Feb 2013 07:38:28 GMT</pubDate></item><item><title>Gwendal Roué - 2013-02-09 01:43:44</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#comments</link><description>
&lt;br /&gt;Some experience around KVC:
&lt;br /&gt;
&lt;br /&gt;GRMustache (&lt;a href="https://github.com/groue/GRMustache"&gt;https://github.com/groue/GRMustache&lt;/a&gt;) is a template engine based to valueForKey:
&lt;br /&gt;
&lt;br /&gt;1. The Mustache (&lt;a href="http://mustache.github.com/mustache.5.html"&gt;http://mustache.github.com/mustache.5.html&lt;/a&gt;) language has a "context stack" where a simple tag such as {{ name }} can send the valueForKey: message to many objects, until one returns a non-nil value, that gets rendered.
&lt;br /&gt;
&lt;br /&gt;Unfortunately, developers don't like NSUndefinedKeyException to stop their debugger when valueForKey: raises an exception. Those exceptions are properly caught and processed, yet developers were thinking some problem did happen. This was an annoyance I had to fix. I had to avoid as much of valueForKey: exceptions as I could.
&lt;br /&gt;
&lt;br /&gt;Swizzling NSObject and NSManagedOject implementations of valueForUndefinedKey:, so that those methods return nil if the receiver is in the set of "quiet" objects for the current thread, was a good enough solution. Mike Ash, I don't know if you remember, but you sent me on the right track on this topic. Thank you for that.
&lt;br /&gt;
&lt;br /&gt;2. NSArray, NSSet and NSOrderedSet have a funny implementation of valueForKey: they return another collection built from the invocation of valueForKey: on their elements. This behavior led to several problems in GRMustache, including the fact that it prevents the simple key "count" to return the number of elements in NSArray, NSSet, etc.
&lt;br /&gt;
&lt;br /&gt;The solution was to detect those class very specifically, and to use the objc_msgSendSuper function. It allows to shunt the implementation of a given selector to the class you provide, particularly to NSObject. And suddenly NSArray has a value for key "count", so does NSSet, etc :-)
&lt;br /&gt;
&lt;br /&gt;As a conclusion:
&lt;br /&gt;
&lt;br /&gt;KeyValueCoding is a strange beast, that is not so badly done: taming it does not require much inner knownledge. I know many developers that try to do a better key-value-Observing. However key-value-Coding does not get much interest: maybe it is just really well done.
&lt;br /&gt;</description><guid isPermaLink="true">826575977cd1c410f6feca64cdeda929</guid><pubDate>Sat, 09 Feb 2013 01:43:44 GMT</pubDate></item><item><title>Fernando - 2013-02-08 22:17:20</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#comments</link><description>It would be nice to mention how collections implement valueForKeyPath: and setValue:forKeyPath: to deal with collection and object operators (@sum, @avg, etc.).
&lt;br /&gt;
&lt;br /&gt;Great article nonetheless.</description><guid isPermaLink="true">cf1df740645a9ad8162d0c09a3ed131b</guid><pubDate>Fri, 08 Feb 2013 22:17:20 GMT</pubDate></item><item><title>Ian - 2013-02-08 16:00:35</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#comments</link><description>@Nico: It occured to me... there is a mechanism to make LLDB aware of things 'after the fact'. You can set an 'expression parser prefix header' like this:
&lt;br /&gt;
&lt;br /&gt;(llbd) set set target.expr-prefix /tmp/myheader.h
&lt;br /&gt;
&lt;br /&gt;You could make it so that when your app generates these dynamic classes, it also writes a header file to some known location. Then you can tell LLDB to read this header from that location using the above command. 
&lt;br /&gt;
&lt;br /&gt;After doing that, I would expect that you would be able to call your dynamic class's methods from the LLDB expression parser without resorting to valueForKey: or any other tricks.</description><guid isPermaLink="true">20be765175c0f848962d27d2488434b7</guid><pubDate>Fri, 08 Feb 2013 16:00:35 GMT</pubDate></item><item><title>Ian - 2013-02-08 15:42:15</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#comments</link><description>@Nico: The reason the debugger doesn't see it as a selector is that the debugger only knows about things that were available to it at compile-time.  I believe if there were another object that had the same selector present at compile time, it *might* work. But AFAIK, the debugger's expression parser won't dig into the Objective-C runtime at expression-parse time.  Someone please correct me if I'm wrong.
&lt;br /&gt;
&lt;br /&gt;A way to prove this to yourself would be to do this: po objc_msgSend(o, NSSelectorFromString(@"property"))
&lt;br /&gt;
&lt;br /&gt;If it works, that would show that the selector is definitively present, but that the debugger doesn't know about it.
&lt;br /&gt;</description><guid isPermaLink="true">916f182dfc69afbc20ad8fa8387765b6</guid><pubDate>Fri, 08 Feb 2013 15:42:15 GMT</pubDate></item><item><title>Raphael - 2013-02-08 15:28:39</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#comments</link><description>I think your &lt;code&gt;setValue:forKey:&lt;/code&gt; code has a tiny mistake: &lt;code&gt;[@"helloYou" capitalizedString]&lt;/code&gt; gives &lt;code&gt;@"Helloyou"&lt;/code&gt;, not &lt;code&gt;@"HelloYou"&lt;/code&gt;, so you won’t find the desired setter method…</description><guid isPermaLink="true">cc2eb17f1a664d7d227157ca285f3b4a</guid><pubDate>Fri, 08 Feb 2013 15:28:39 GMT</pubDate></item><item><title>Nico - 2013-02-08 15:27:42</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#comments</link><description>Hello!
&lt;br /&gt;
&lt;br /&gt;Great article! I have a book of yours that's full of them =P
&lt;br /&gt;
&lt;br /&gt;I have a question (it may not be related to the subject of the article, sorry if that's the case) regarding dot syntax.
&lt;br /&gt;
&lt;br /&gt;I wrote a small piece of code that, given an XML file with a certain format, dynamically creates a class for the object type described by it.
&lt;br /&gt;
&lt;br /&gt;It works ok, but I was wondering why is it that, when using the debugger to query the properties, neither &lt;code&gt;o.property&lt;/code&gt; nor &lt;code&gt;[o property]&lt;/code&gt; works, but &lt;code&gt;[o valueForKey:@"property"]&lt;/code&gt; does indeed work (this article helped me understand that it is because of the property-&amp;gt;ivar-&amp;gt;underscored ivar fallback).
&lt;br /&gt;
&lt;br /&gt;When creating the class, I'm adding both ivars and properties for it.
&lt;br /&gt;
&lt;br /&gt;I tried adding getter methods (just getters for now) in some sort of generic way but I can't seem to understand how to retrieve ivar values without going through the ivar list every time.
&lt;br /&gt;
&lt;br /&gt;Any pointers on how I may enable such feature for my dynamically-generated classes? I know it may not be possible at all to use dot syntax, but I'm curious about the sending the getter message to the object.
&lt;br /&gt;
&lt;br /&gt;Sorry if this is either too long, too offtopic or plainly too twisted to read!</description><guid isPermaLink="true">7e4c05d5256ec84c2001546540cc86f2</guid><pubDate>Fri, 08 Feb 2013 15:27:42 GMT</pubDate></item></channel></rss>
