4

In Key-Value Coding Programming Guide it states NSObject subclasses are KVC compliant.

Swift objects that inherit from NSObject or one of its subclasses are key-value coding compliant for their properties by default

Can custom objects(struct, classes) adopt NSKeyValueCoding and be KVC compliant? Also, how is KVC given to an object just by subclassing NSObject?

2
  • I'm embarrassed. I deleted my "KVO" answer -- please ignore! ;-) Commented May 30, 2017 at 19:39
  • No problem, the two are so confusing cause of the abbreviations are so similar! @joeybladb Commented May 30, 2017 at 19:40

1 Answer 1

4

Unlike a formal protocol, that any object could technically conform to, NSKeyValueCoding is available to any NSObject via Informal Protocols:

An informal protocol is a category on NSObject, which implicitly makes almost all objects adopters of the protocol. (A category is a language feature that enables you to add methods to a class without subclassing it.) Implementation of the methods in an informal protocol is optional. Before invoking a method, the calling object checks to see whether the target object implements it. Until optional protocol methods were introduced in Objective-C 2.0, informal protocols were essential to the way Foundation and AppKit classes implemented delegation.

This is as opposed to simply implementing the KVC directly into NSObject, I think the main benefit of the informal protocol is to split up the functionality of NSObject into separate files. But there may be other benefits of using Informal Protocols

And because NSKeyValueCoding is a category on NSObject, you unfortunately cannot just make any custom object support KVC

Sign up to request clarification or add additional context in comments.

12 Comments

You could make a custom non-NSObject inheriting class (on Apple platforms) support KVC – you just need to implement (and expose to Obj-C) the methods detailed by the informal NSKeyValueCoding protocol. Not that I would ever recommend doing that besides for a learning exercise though.
@Hamish could you post an example of this? If it is possible that would be the correct answer. I am asking about the possibility of it in Swift without NSObject subclass and how it "just" works on an NSObject.
Interesting point, how would you do this? I didn't think you could? Because NSKeyValueCoding is actually a category (aka informal protocol in this context), it's implementation is already done. So short of just overriding -setValue:forKey:, and whatever else, I don't think you can just make any custom object conform right?
Here's a (really) crude example of how you could do it (it doesn't take into account property setting and bunch of other things): gist.github.com/hamishknight/3e804b9ba9e722c84551f3fef0183d0f.
@AO so KVC "just" works on NSObjects because the informal protocol is already implemented?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.