mikeash.com pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html commentshttp://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#commentsmikeash.com Recent CommentsFri, 29 Mar 2024 06:27:39 GMTPyRSS2Gen-1.0.0http://blogs.law.harvard.edu/tech/rssIvan - 2015-09-30 23:09:59http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#commentsHi Mike, thank you for your great articles, carry on writing please! <br /> <br />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 <code>is</code>, thus the complete search path is: setter/getter -&gt; <code>_&lt;Key&gt; -&gt; _is&lt;Key&gt; -&gt; &lt;Key&gt; -&gt; is&lt;Key&gt;</code>. <br /> <br />Actually there is many of other interesting things about searching algorithms in KVC, all of them are describet here <a href="http://apple.co/1KSAh1t">http://apple.co/1KSAh1t</a>ef84382a1f0e5ec2e9831d29031cb992Wed, 30 Sep 2015 23:09:59 GMTRecovery - 2014-10-20 14:01:03http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#commentsWhat's the best way to pass values to different video controllers, KVC, Delegate or something else?78b0ff841c27a699a1d91c4c0def2b72Mon, 20 Oct 2014 14:01:03 GMTMorty Eraser - 2014-10-16 15:33:55http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#commentsThis 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.3f2c487d43c9baeaf3f1dec75e71870eThu, 16 Oct 2014 15:33:55 GMTCarick - 2014-10-13 16:34:10http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#commentsI 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. <br /> <br />BTW, really great tutorial. I need to spend some time to fully digest the content.3ca2a88f7adc6a30f4724e08ef083bfaMon, 13 Oct 2014 16:34:10 GMTManoj - 2013-03-25 10:04:29http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#commentsGreat article !! <br /> <br />Always impressed the way you put down things in simple but in precise. <br />a520724e1b84573823b53c88c1d17fbdMon, 25 Mar 2013 10:04:29 GMTGeorge - 2013-02-16 15:31:24http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#commentsMike, 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! <br />3b416f7e5e22ec4e443e9f5c3c4b20f4Sat, 16 Feb 2013 15:31:24 GMTMatthias Plappert - 2013-02-13 06:55:16http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#commentsGreat article. One thing that seems to be missing are @properties with a custom getter, e.g. <br /><div class="blogcommentquote"><div class="blogcommentquoteinner">@propert (assign, getter=isEnabled) BOOL enabled;</div></div> <br />Any insight how does work with KVC?8ae0284e3eec96f55224ece16b0bf099Wed, 13 Feb 2013 06:55:16 GMTJustin Spahr-Summers - 2013-02-10 07:38:28http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#commentsGreat article! <br /> <br />It's worth noting that KVC will only access ivars if +accessInstanceVariablesDirectly isn't overridden to return NO: <a href="https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Protocols/NSKeyValueCoding_Protocol/Reference/Reference.html#//apple_ref/occ/clm/NSObject/accessInstanceVariablesDirectly">https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Protocols/NSKeyValueCoding_Protocol/Reference/Reference.html#//apple_ref/occ/clm/NSObject/accessInstanceVariablesDirectly</a> <br /> <br />The behavior of nil when using KVC to set a primitive value is also somewhat interesting.0ad1be7403f2d53faeba83dd992ac6c9Sun, 10 Feb 2013 07:38:28 GMTGwendal Roué - 2013-02-09 01:43:44http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#comments <br />Some experience around KVC: <br /> <br />GRMustache (<a href="https://github.com/groue/GRMustache">https://github.com/groue/GRMustache</a>) is a template engine based to valueForKey: <br /> <br />1. The Mustache (<a href="http://mustache.github.com/mustache.5.html">http://mustache.github.com/mustache.5.html</a>) 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. <br /> <br />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. <br /> <br />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. <br /> <br />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. <br /> <br />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 :-) <br /> <br />As a conclusion: <br /> <br />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. <br />826575977cd1c410f6feca64cdeda929Sat, 09 Feb 2013 01:43:44 GMTFernando - 2013-02-08 22:17:20http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#commentsIt would be nice to mention how collections implement valueForKeyPath: and setValue:forKeyPath: to deal with collection and object operators (@sum, @avg, etc.). <br /> <br />Great article nonetheless.cf1df740645a9ad8162d0c09a3ed131bFri, 08 Feb 2013 22:17:20 GMTIan - 2013-02-08 16:00:35http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#comments@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: <br /> <br />(llbd) set set target.expr-prefix /tmp/myheader.h <br /> <br />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. <br /> <br />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.20be765175c0f848962d27d2488434b7Fri, 08 Feb 2013 16:00:35 GMTIan - 2013-02-08 15:42:15http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#comments@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. <br /> <br />A way to prove this to yourself would be to do this: po objc_msgSend(o, NSSelectorFromString(@"property")) <br /> <br />If it works, that would show that the selector is definitively present, but that the debugger doesn't know about it. <br />916f182dfc69afbc20ad8fa8387765b6Fri, 08 Feb 2013 15:42:15 GMTRaphael - 2013-02-08 15:28:39http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#commentsI think your <code>setValue:forKey:</code> code has a tiny mistake: <code>[@"helloYou" capitalizedString]</code> gives <code>@"Helloyou"</code>, not <code>@"HelloYou"</code>, so you won’t find the desired setter method…cc2eb17f1a664d7d227157ca285f3b4aFri, 08 Feb 2013 15:28:39 GMTNico - 2013-02-08 15:27:42http://www.mikeash.com/?page=pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html#commentsHello! <br /> <br />Great article! I have a book of yours that's full of them =P <br /> <br />I have a question (it may not be related to the subject of the article, sorry if that's the case) regarding dot syntax. <br /> <br />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. <br /> <br />It works ok, but I was wondering why is it that, when using the debugger to query the properties, neither <code>o.property</code> nor <code>[o property]</code> works, but <code>[o valueForKey:@"property"]</code> does indeed work (this article helped me understand that it is because of the property-&gt;ivar-&gt;underscored ivar fallback). <br /> <br />When creating the class, I'm adding both ivars and properties for it. <br /> <br />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. <br /> <br />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. <br /> <br />Sorry if this is either too long, too offtopic or plainly too twisted to read!7e4c05d5256ec84c2001546540cc86f2Fri, 08 Feb 2013 15:27:42 GMT