key-value-observing

KVO: +keyPathsForValuesAffecting<Key> doesn't work with (subclass of) NSObjectController

为君一笑 提交于 2019-12-07 05:28:35
问题 I have a KVO-able class (call it Observee ), which affectedValue dynamic property is affected by affectingValue property. The dependency between the properties is defined by implementing +keyPathsForValuesAffectingAffectedValue method. Setting a value to affectingValue notifies that affectedValue has changed as I expected, unless Ovservee is a subclass of NSObjectController . Full example follows: @interface Observee : NSObject // or NSObjectController @property (readonly, strong, nonatomic)

Key-Value Coding

元气小坏坏 提交于 2019-12-07 04:25:10
问题 I had a straight forward approach of turning Key/Value pairs of an XML excerpt into an NSDictionary when I started a simple iPhone application. The problem is, I need to turn those NSDictionary's instances that once populated my UITableView's into custom classes because they require behavior and additional complexity. The problem here is that now in order for me to instantiate an object and fill its instance variables with key/value pairs from a web service becomes that much more difficult. I

Is it possible to set up KVO notifications for static variables in objective C?

谁都会走 提交于 2019-12-07 03:27:35
问题 I have class A, with instance variables deriving attributes from cached data, this cached data is represented as a singleton and is part of A (it's essentially a NSDictionary). From time to time, this cache is modified. When this happens, I would like to have all A instances pull new information from the cache the next time they access their attribute , or in other words, invalidate their attribute content. Up to now, each instances is notified manually (using a static array to track members)

NSSortDescriptor to sort by number of items in Core Data To-Many Relationships

时间秒杀一切 提交于 2019-12-07 02:04:16
问题 It's a long standing problem when using Core Data to-many-relationships that it is very hard to sort a fetch request using NSSortDescriptor on a Parent entity based on the number of children are in a one-to-many relationship to a Child entity. This is especially useful in combination with a NSFetchedResultsController . Typically initializing the sort descriptor as: NSSortDescriptor *sortByNumberOfChildren = [[NSSortDescriptor alloc] initWithKey:@"children.@count" ascending:NO]; results in an

Bool Property Cannot be marked dynamic in swift

偶尔善良 提交于 2019-12-06 21:03:40
问题 I'm trying to observe Bool value in swift using KVO and add dynamic modifier like this : dynamic var isRestricted:Bool? and the compiler say Property cannot be marked dynamic because its type canot be represented in Objective-C then what should I do? should I change to NSNumber for this? and What is the best practice for observing value then? im using xcode 7 beta 2 回答1: The actual problem is that optional booleans cannot be represented in Objective-C (and therefore not marked dynamic). Using

KVO doesn't work with keypath like com.alpha.

谁都会走 提交于 2019-12-06 16:40:49
My NSMutableDictionary contains simple keys (@"one", @"two", @"three") and complex keys (@"com.alpha", @"com.beta"). Is it possible to use observers for a complex key? Observers work well with simple keys, but didn't worked with complex keys. What is a solution? [self.dict addObserver:self forKeyPath:@"com.alpha" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil]; -(IBAction) onChange:(id)sender { [self.dict setObject:@"newValue" forKey:@"com.alpha"]; } -(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:

KVO crash on UITextField initWithFrame

风流意气都作罢 提交于 2019-12-06 15:05:01
A customer of my app sent me this strange crash report and I cannot figure out how this could ever crash. I cannot reproduce the problem myself, so am reliant on the crash report. It crashes on the following very simple line of code: UITextField *value = [[UITextField alloc] initWithFrame:frame]; The crash seems to be related to key-value-observing (KVO), but I am not doing anything with that here; it's a simply constructor! The only possibility I could see, is that another thread is writing to a dealloc'ed object that happens to have the same memory address as an object that is created by the

How to create a binding for NSApp.dockTile's

倾然丶 夕夏残阳落幕 提交于 2019-12-06 12:27:14
In IB it is easy to bind a label or text field to some controller's keyPath. The NSDockTile (available via [[NSApp dockTile] setBadgeLabel:@"123"]) doesn't appear in IB, and I cannot figure out how to programmatically bind its "badgeLabel" property like you might bind a label/textfield/table column. Any ideas? NSDockTile doesn't have any bindings, so your controller will have to update the dock tile manually. You could do this using KVO which would have the same effect as binding it. Create a context as a global: static void* MyContext=(void*)@"MyContext"; Then, in your init method:

Cocoa: Avoiding 'Updates Continuously' in control binds

别等时光非礼了梦想. 提交于 2019-12-06 11:28:07
I have several panels that contain NSTextField controls bound to properties within the File's Owner object. If the user edits a field and then presses Tab , to move to the next field, it works as expected. However if the user doesn't press Tab and just presses the OK button, the new value is not set in the File's Owner object. In order to workaround this I have set Updates Continuously in the binding, but this must be expensive (EDIT: or at least it's inelegant). Is there a way to force the bind update when the OK button is pressed rather than using Updates Continuously ? You're right that you

Draw line between two movable uiviews

て烟熏妆下的殇ゞ 提交于 2019-12-06 06:05:10
I have a "scrollview" with nodes ( UIViews ) that can be dragged around. I am trying to draw edges between selected UIViews with a "calayer", but I can't figure out how to redraw the line when a views position have changed. In my viewController class I add the edge between first and second in the nodes array as: EdgeLayer *edge = [[EdgeLayer alloc]init]; edge.delegate = self; edge.strokeColor = [UIColor colorWithWhite:0.25 alpha:1.0]; edge.strokeWidth = 0.5; edge.startNode = [nodes objectAtIndex:0]; edge.endNode = [nodes objectAtIndex:1]; edge.frame = scrollView.bounds; [scrollView.layer