key-value-observing

Closure identity in swift: unregister observing closure

余生长醉 提交于 2019-12-12 19:19:52
问题 When rethinking my everyday programming patterns to be more swifty , there is one, that I really struggle with: observing changes . After a lot of thinking and research I have yet to find a satisfying solution . That is one, that is easy to use, leverages the full potential of swift's strong type system, is compatible with value types and allows for static dispatch. Maybe the latter one is not possible and that's the reason why my search is unsuccessful so far. If so, I would like to know why

KVO listener issues in Swift 4

回眸只為那壹抹淺笑 提交于 2019-12-12 16:28:56
问题 I am using ViewModel class and want to setup observer if any changes into loginResponse variable. @objcMembers class ViewModel: NSObject { var count = 300 @objc dynamic var loginResponse :String override init() { loginResponse = "1" super.init() setupTimer() } func setupTimer(){ _ = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector:#selector(callTimer), userInfo: nil, repeats: true) } func callTimer(){ let minutes = String(count / 60) let seconds = String(count % 60)

Observing the region of MKMapView via KVO?

懵懂的女人 提交于 2019-12-12 12:13:30
问题 I have an object that is interested in knowing when the region of a MKMapView is changed. This object is not the delegate of the map view, however. I'm trying the following, where map is a MKMapView: [map addObserver:self forKeyPath:@"region" options:0 context:nil]; However, observeValueForKeyPath:ofObject:change:context: isn't being called back. As an interim solution, I have the map's delegate letting this other object know when the map region is changed, but I'd like to uncouple the two

Cocoa binding to a particular item in an array controller

会有一股神秘感。 提交于 2019-12-12 12:07:28
问题 Is it possible using NSArrayController to bind a NSTextField 's value to a particular item in the array? In particular, I want to bind to a property on the first item in the array, and show nothing if the array is empty. Using arrangedObjects.command shows just "(" -- presumably it's trying to show a multi-line string with comma-separated strings for each item. I just want the first one. 回答1: Bind the text field to selection.command , and programmatically set the array controller's selection

How to add a KVO to MPMoviePlayer currentPlaybackTime?

被刻印的时光 ゝ 提交于 2019-12-12 08:48:26
问题 How can I add a KVO to the currentPlaybackTime property of a MPMoviePlayer class? 回答1: You cannot add a KVO to currentPlaybackTime since the property is not explicitly declared as KVO compatible. Instead, you could try polling the player regularly and storing the position, with code such as: - (void) BeginPlayerPolling { self.pollPlayerTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(PollPlayerTimer_tick:) userInfo:nil repeats:YES]; } - (void) PollPlayerTimer

Swift 4 Using KVO to listen to volume changes

☆樱花仙子☆ 提交于 2019-12-12 08:21:58
问题 I just updated to Swift 4 and Xcode 9 and got a (swiftlint) warning for the following code telling me that I should use KVO now: Warning: (Block Based KVO Violation: Prefer the new block based KVO API with keypaths when using Swift 3.2 or later. (block_based_kvo)) The old code: override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { if keyPath == "outputVolume"{ guard let newKey = change?

Can/How Should I replace my KVO stuff with RC3?

Deadly 提交于 2019-12-12 07:35:54
问题 I'm trying to port an objc app which uses Facebook's KVOController, to Swift. I've been encouraged to look at RC3 as an alternate and more Swiftish approach. I've read some blogs and I'm encouraged to give this a try. But much of the docs and blogs seem to concentrate on sockets and timers as examples. So I have two simple questions right now: 1) Given an objc fragment like: [self.KVOController observe: self.site keyPath: @"programs" options: NSKeyValueObservingOptionInitial block:^(id

Errors of key value observers and NSKVODeallocateBreak

南楼画角 提交于 2019-12-12 06:23:08
问题 I'm implementing a UITableView as a first level view controller that contains 5 table cells. Hitting on any of these cells will present a second level view. At the top left of this level view, there is a "back" button to return to the first level view. At the second level view, swiping left or right will show adjacent views continuously that link to those adjacent table cells at the first level view. After running, from first level view to second level view it's ok. But when hitting the "back

Is it safe to use Interface Builder bindings to observe properties changed on non-main thread?

元气小坏坏 提交于 2019-12-12 05:44:57
问题 If I use Interface Builder to bind, e.g. NSTextField 's value to someObject.property and this property (assuming it's assign, atomic type) will change from a non-main thread, will this be safe/correct? Cocoa generally forbids updating UI from non-main thread, so I wonder whether the binding mechanism automagically schedules updates to happen on the main thread for me, or is it unsafe to bind directly to objects that may be changed from other threads. Does the same hold true if I fire KVO

Enhance my Core Data design. Experts only!

三世轮回 提交于 2019-12-12 05:19:17
问题 In AcaniUsers, I'm downloading the closest 20 users to me and displaying their profile pictures as thumbnails in a table view. User & Photo are both Resources because they each have an id (MongoDB BSON ObjectId) on the server. Each user has a unique_id. Each Photo has four different sizes (images) on the server: square: 75x75, square@2x: 150x150, large: 320x480, large@2x: 640x960. But, each device will only have two of these sizes, depending on whether it's an iPhone 3 or 4 (retina display).