key-value-observing

Objective-C variable… pointing to itself?

喜夏-厌秋 提交于 2019-12-03 08:58:55
问题 I spotted this construct in some of Apple's example code for dealing with key-value observing. When adding an observer, you can add a context (in the form of a void* variable) that can uniquely identify the KVO call - particularly useful if you want multiple KVO calls to trigger the same action, as the single context can avoid using a bunch of chained or statements to check all the possibilities. This is the line that's used to declare the variable used for the context: static void *aContext

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

我是研究僧i 提交于 2019-12-03 08:26:18
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 observer, id object, NSDictionary *change) { [self.tableView reloadData]; }]; What is the simple way to

Array of NSManagedObject attributes

落爺英雄遲暮 提交于 2019-12-03 06:24:18
I'd like to get an array of the attributes for my NSManagedObject so I can use KVO to export them. I can create an array manually and then iterate through it, however, I'd like to get this list automatically, then iterate. An NSManagedObject has an entity associated with it. Use NSEntityDescription 's -attributesByName and -relationshipsByName . You'll get a dictionary back from each of those methods. Just ask the dicts for their -allKeys . Thanks Joshua. Here's code that I used in case any one would like to see a hard example: NSString *entityName = NSStringFromClass([myEntity class]);

KVO Notifications for a Modification of an NSArray backed by a NSMutableArray

一个人想着一个人 提交于 2019-12-03 05:19:23
问题 I am trying to use KVO to listen to collection change events on an NSArray property. Publicly, the property is a readonly NSArray, but is backed by an NSMutableArray ivar so that I can modify the collection. I know I can set the property to a new value to get a “set” change, but I’m interested in add, remove, replace changes. How do I correctly notify these type of changes for an NSArray? @interface Model : NSObject @property (nonatomic, readonly) NSArray *items; @end @implementation Model {

observing contentSize (CGSize) with KVO in swift

橙三吉。 提交于 2019-12-03 03:26:36
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 === collectionView && keyPath! == "contentSize" { print(change) } } } and in xcode terminal I got a NSSize not

NSManagedObject and KVO vs Documentation

六眼飞鱼酱① 提交于 2019-12-03 01:29:28
I have a custom NSManagedObject subclass, say, Person . I also have a UIView registered with -addObserver:forKeyPath:options:context: to observe various properties of a Person , some of which are persistent like "name" and others are just dumb KVO-compliant accessors unrelated to Core Data, like "drinking". @interface Person : NSManagedObject { BOOL drinking; } @property (nonatomic, retain) NSString* name; @property (nonatomic, readonly) BOOL drinking; @end @implementation Person @dynamic name; ... - (void) getDrunk { [self willChangeValueForKey: @"drinking"]; drinking = YES; [self

Simple KVO example

久未见 提交于 2019-12-03 01:04:07
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 { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction

iPhone KVO between two classes

谁说胖子不能爱 提交于 2019-12-03 00:57:19
问题 I have two classes in my app class A and Class B. Both class A and B are instances of UIViewController. Class A has a button that when pushed pushes class B onto the stack. Class B has a string that class A would like to observe and update it's interface with as needed. I've been able to use: [self addObserver:self forKeyPath:@"name" options:0 context:NULL]; in class B to view the changes to the string. When i try and use the following in class A viewWillAppear method: ClassB *b = [[ClassB

NSProxy and Key Value Observing

╄→гoц情女王★ 提交于 2019-12-02 20:50:46
NSProxy seems to work very well as stand-in objects for those that don't yet exist. For example. - (NSMethodSignature *)methodSignatureForSelector:(SEL)sel { return [self.target methodSignatureForSelector:sel]; } - (void)forwardInvocation:(NSInvocation *)invocation { [invocation invokeWithTarget:self.target]; } The above code will transparently pass any method invocation to the target that the proxy represents. However, it doesn't seem to handle KVO observations and notifications on the target. I tried to use a NSProxy subclass as standing for objects to be passed to NSTableView , but I'm

Cocoa Key Value Bindings: What are the explanations of the various options for Controller Key?

微笑、不失礼 提交于 2019-12-02 19:38:58
When I bind a control to an NSArrayController using Interface Builder, there are a variety of options under the "Controller Key" field in the bindings inspector. I understand what "arrangedObjects" is, and I semi-understand what "selection" is, but I'd love to see a really nice explanation of all the options and when to use each one. The list includes: selectionIndexes, selectionIndex, selectedObject, sortDescriptors, etc. I haven't been able to find a good explanation of these options. I'm having trouble with a button that's bound to target > selection, so I'm hoping a much deeper