If so, are there any key differences that weren't otherwise present when using key-value observation in Objective-C?
相关问题
- “Zero out” sensitive String data in Swift
- SwiftUI: UIImage (QRCode) does not load after call
- Get the NSRange for the visible text after scroll
- UIPanGestureRecognizer is not working in iOS 13
- What does a Firebase observer actually do?
相关文章
- Using if let syntax in switch statement
- Enum with associated value conforming to CaseItera
- Swift - hide pickerView after value selected
- Is there a Github markdown language identifier for
- How can I vertically align my status bar item text
- Adding TapGestureRecognizer to UILabel in Swift
- Attempt to present UIAlertController on View Contr
- Swift - Snapshotting a view that has not been rend
One important thing to mention is that after updating your Xcode to 7 beta you might be getting the following message: "Method does not override any method from its superclass". That's because of the arguments' optionality. Make sure that your observation handler looks exactly as follows:
This may be prove helpful to few people -
I had used KVO in this way in Swift 3. You can use this code with few changes.
Another example for anyone who runs into a problem with types such as Int? and CGFloat?. You simply set you class as a subclass of NSObject and declare your variables as follows e.g:
Yes.
KVO requires dynamic dispatch, so you simply need to add the
dynamic
modifier to a method, property, subscript, or initializer:dynamic var foo = 0
The
dynamic
modifier ensures that references to the declaration will be dynamically dispatched and accessed throughobjc_msgSend
.You can use KVO in Swift, but only for
dynamic
properties ofNSObject
subclass. Consider that you wanted to observe thebar
property of aFoo
class. In Swift 4, specifybar
asdynamic
property in yourNSObject
subclass:You can then register to observe changes to the
bar
property. In Swift 4 and Swift 3.2, this has been greatly simplified:Note, in Swift 4, we now have strong typing of keypaths using the backslash character (the
\.bar
is the keypath for thebar
property of the object being observed). Also, because it's using the completion closure pattern, we don't have to manually remove observers (when thetoken
falls out of scope, the observer is removed for us) nor do we have to worry about calling thesuper
implementation if the key doesn't match. The closure is called only when this particular observer is invoked. For more information, see WWDC 2017 video, What's New in Foundation.In Swift 3, to observe this, it's a bit more complicated, but very similar to what one does in Objective-C. Namely, you would implement
observeValue(forKeyPath keyPath:, of object:, change:, context:)
which (a) makes sure we're dealing with our context (and not something that oursuper
instance had registered to observe); and then (b) either handle it or pass it on to thesuper
implementation, as necessary. And make sure to remove yourself as an observer when appropriate. For example, you might remove the observer when it is deallocated:In Swift 3:
Note, you can only observe properties that can be represented in Objective-C. Thus, you cannot observe generics, Swift
struct
types, Swiftenum
types, etc.For a discussion of the Swift 2 implementation, see my original answer, below.
Using the
dynamic
keyword to achieve KVO withNSObject
subclasses is described in the Key-Value Observing section of the Adopting Cocoa Design Conventions chapter of the Using Swift with Cocoa and Objective-C guide:[Note, this KVO discussion has subsequently been removed from the Using Swift with Cocoa and Objective-C guide, which has been adapted for Swift 3, but it still works as outlined at the top of this answer.]
It's worth noting that Swift has its own native property observer system, but that's for a class specifying its own code that will be performed upon observation of its own properties. KVO, on the other hand, is designed to register to observe changes to some dynamic property of some other class.
Yes and no. KVO works on NSObject subclasses much as it always has. It does not work for classes that don't subclass NSObject. Swift does not (currently at least) have its own native observation system.
(See comments for how to expose other properties as ObjC so KVO works on them)
See the Apple Documentation for a full example.