KVO/KVC in Swift


(Colin Cornaby) #1

I looked through the Swift Foundation this morning to see if there was evidence of KVO/KVC, but I didn't find any. Apologies if I missed anything. I'm assuming KVO/KVC is still incompatible with pure Swift, but I was looking for evidence that maybe something had been added in NSObject.

We work in a lot of real time/signal-ly code so we've gotten a lot of good use out of KVO/KVC in Cocoa. I've seen a lot of value in using it when doing UI work as well, either through bindings on OS X (which seems a little unloved these days), or directly by observing key values on iOS. The willSet/didSet functions in Swift is a great step forward for variables in having some of this behavior, but it's still messy in situations where you are dealing with classes you don't control. For example, we're an API vendor, and KVC/KVO is handy for providing a way for a customer to observe changes. But for a pure Swift API we'd have to provide and maintain some sort of larger callback structure, or have a workflow that encourages subclassing with customers adding their own getter/setter handlers.

Are there any plans to add anything like KVO or KVC to pure Swift, with maybe something the bridges to Obj-C KVO? Swift doesn't need to go down the road of full Obj-C style dynamism with method swizzling and all that. But some sort of key based inspection of objects, and language construct for getting callbacks on a property change would be great.


(Chris Lattner) #2

Hi Colin,

This is one of *many* things that we are interested in working on some day, but we need to stay focused on shorter term goals (like getting to ABI stability) to make sure we are successful with those. If you’re working on Apple platforms, Swift does interoperate with the Objective-C KVO implementation, using the “dynamic" keyword.

-Chris

···

On Dec 3, 2015, at 12:49 PM, Colin Cornaby <colin.cornaby@mac.com> wrote:

I looked through the Swift Foundation this morning to see if there was evidence of KVO/KVC, but I didn't find any. Apologies if I missed anything. I'm assuming KVO/KVC is still incompatible with pure Swift, but I was looking for evidence that maybe something had been added in NSObject.
..

Are there any plans to add anything like KVO or KVC to pure Swift, with maybe something the bridges to Obj-C KVO?


(Adrian Kashivskyy) #3

Colin,

Until KVC/KVO is supported, you may be interested in using constructs like Observable<T>. Here are some illustrative libraries that make use of it: SwiftBond/Bond <https://github.com/SwiftBond/Bond>, slazyk/Observable-Swift <https://github.com/slazyk/Observable-Swift>.

Pozdrawiam – Regards,
Adrian Kashivskyy

···

Wiadomość napisana przez Chris Lattner <clattner@apple.com> w dniu 03.12.2015, o godz. 22:00:

On Dec 3, 2015, at 12:49 PM, Colin Cornaby <colin.cornaby@mac.com> wrote:

I looked through the Swift Foundation this morning to see if there was evidence of KVO/KVC, but I didn't find any. Apologies if I missed anything. I'm assuming KVO/KVC is still incompatible with pure Swift, but I was looking for evidence that maybe something had been added in NSObject.
..

Are there any plans to add anything like KVO or KVC to pure Swift, with maybe something the bridges to Obj-C KVO?

Hi Colin,

This is one of *many* things that we are interested in working on some day, but we need to stay focused on shorter term goals (like getting to ABI stability) to make sure we are successful with those. If you’re working on Apple platforms, Swift does interoperate with the Objective-C KVO implementation, using the “dynamic" keyword.

-Chris

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution