key-value-observing

Reading preferences set by UIAutomation's UIAApplication.setPreferencesValueForKey() on the target device?

我的梦境 提交于 2019-12-04 15:47:54
Over the last few days I've been playing with Apple's UIAutomation framework in an attempt to try to put together a suite of acceptance tests to drive the development of an app I'm working on (in a BDD type way...). One thing I'm bumping up against is how to get the SUT into a given state so I can begin my tests if I need to set some internal state for that to happen. The problem is that Apple's Javascript environment doesn't provide any functionality I could use to communicate with the device other than through it's UI (I'm sure this is probably by design but sometimes this is just

observing contentSize (CGSize) with KVO in swift

这一生的挚爱 提交于 2019-12-04 09:54:32
问题 I'm trying to observering collectionView.contentSize like this : func startObserveCollectionView() { collectionView.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.Old.union(NSKeyValueObservingOptions.New), context: &SearchDasboardLabelContext) } override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) { if context == &SearchDasboardLabelContext { if object ===

Performance hit incurred using NSMutableDictionary vs. NSMutableArray>

喜夏-厌秋 提交于 2019-12-04 09:36:23
I am considering using an NSMutableDictionary in place of my current NSMutableArray. This is primarily for KVC/KVO reasons. The collection will undergo heavy mutation within the inner loop of my drawing method. Can I expect to incur a significant performance hit if I go ahead with this replacement? Cheers, Doug The only way to be sure is to measure. None of us have enough knowledge about how NSMutableDictionary's and NSMutableArray's implementations work, so there's little point asking. Granted, you could probably expect some hit since the dictionary has to do additional hashing that a simple

Simple KVO example

隐身守侯 提交于 2019-12-04 09:06:34
问题 I am trying to do simple KVO example, but I am having problems. This is my *.m file: #import "KVO_ViewController.h" @interface KVO_ViewController () @property NSUInteger number; @end @implementation KVO_ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self addObserver:self forKeyPath:@"number" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil]; } - (void)didReceiveMemoryWarning {

Using NSKeyValueObservation to observe value in UserDefaults

纵然是瞬间 提交于 2019-12-04 04:38:36
I would like to use the block-based KVO from Swift 4 to observe changes to a value in UserDefaults . I am able to do this for observing a key path for WKWebView 's estimatedProgress but haven't been successful with UserDefaults because the provided key path isn't what it's looking for. Providing just a String isn't enough (Generic parameter 'Value' could not be inferred), prefixing it with \ isn't enough (Type of expression is ambiguous without more context). What's the correct way to create the KeyPath to observe a value in UserDefaults ? observerToken = UserDefaults.standard.observe("myvalue

KVO versus NSNotifications [duplicate]

泄露秘密 提交于 2019-12-04 03:07:27
This question already has an answer here: NSNotification VS KVO 1 answer Is there any advantage to use KVO instead of the more "generic" (and to my opion more robust) feature of NSNotification s ? I hate KVO with passion, mainly because it forces me to route all KVO notifications through a single handler. I use whatever else available if I have the choice. But KVO has the distinct advantage of being available for many of the classes in the standard library – if you want to observe property changes on some classes from the standard library, KVO might be your only option. There is one very

What's a good way to bind from a shared utility window and the frontmost document window?

眉间皱痕 提交于 2019-12-04 00:37:31
I have an application which allows for multiple NSDocuments to be open. In this application is a single utility window that contains some functionality that I want to apply to the frontmost document. I am trying to use bindings here, so the trick is how to cleanly bind the user interface of the utility window to the frontmost document. The goal is that then switching the frontmost document window will update the view in the utility window; controls that are bound to properties of the frontmost document's model would be updated appropriately when state changes in the document's model, etc. For

Swift 4 Using KVO to listen to volume changes

无人久伴 提交于 2019-12-04 00:15:39
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?[NSKeyValueChangeKey.newKey] as? NSNumber else { fatalError("Could not unwrap optional content of new key") } let

Send Notification When a Property is Changed Using KVO

﹥>﹥吖頭↗ 提交于 2019-12-03 23:27:47
I had a property named myName in my class, like: @property (nonatomic, strong) NSString *myName; I need to send a notification when the myName property's value is changed. Now I'm doing something like: - (void)setMyName:(NSString *)name { _myName = name; [[NSNotificationCenter defaultCenter] postNotificationName:CHANGE_NOTIFICATION object:nil]; } I know there is something like Key-Value Observing in iOS. But I don't know how to implement it, I read the entire document, but couldn't get a good understanding. Please help me to understand how to implement the same without using custom setter.

Cocoa key value observing a key/entry in a dictionary

女生的网名这么多〃 提交于 2019-12-03 23:16:38
Is it possible to observe a specific key in a dictionary? If so how can I do it? Yes (although it only makes sense to be observing an NSMutableDictionary ). @interface Foo : NSObject @end @implementation Foo - (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { NSLog(@"observing: -[%@ %@]", object, keyPath); NSLog(@"change: %@", change); } @end int main (int argc, const char * argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; Foo * f = [[Foo alloc] init]; NSMutableDictionary * d = [NSMutableDictionary