NSTreeController KVO notifications unexpectedly not firing - objective-c

I've encountered a bit of a poser involving NSTreeController and KVO. NSTreeController's selectionIndexPaths property is documented as being KVO-observable—and when I observe it directly, it works perfectly. However, if I list NSTreeController's selectionIndexPath as a dependency of some other property, and then try to observe that, the notifications are not fired when one would expect.
Here's the shortest sample code I could come up with to demonstrate what I mean:
import Cocoa
class ViewController: NSViewController {
// Our tree controller
#IBOutlet dynamic var treeController: NSTreeController!
// Some random property on my object; you'll see why it's here later
#objc dynamic var foo: String = "Foo"
// A quick-and-dirty class to give us something to populate our tree with
class Thingy: NSObject {
#objc let name: String
init(_ name: String) { self.name = name }
#objc var children: [Thingy] { return [] }
}
// The property that the tree controller's `Content Array` is bound to
#objc dynamic var thingies: [Thingy] = [Thingy("Foo"), Thingy("Bar")]
// Dependencies for selectionIndexPaths
#objc private static let keyPathsForValuesAffectingSelectionIndexPaths: Set<String> = [
#keyPath(treeController.selectionIndexPaths),
#keyPath(foo)
]
// This property should be dependent on the tree controller's selectionIndexPaths
// (and also on foo)
#objc dynamic var selectionIndexPaths: [IndexPath] {
return self.treeController.selectionIndexPaths
}
// Some properties to store our KVO observations
var observer1: NSKeyValueObservation? = nil
var observer2: NSKeyValueObservation? = nil
// And set up the observations
override func viewDidLoad() {
super.viewDidLoad()
self.observer1 = self.observe(\.selectionIndexPaths) { _, _ in
print("This is only logged when foo changes")
}
self.observer2 = self.observe(\.treeController.selectionIndexPaths) { _, _ in
print("This, however, is logged when the tree controller's selection changes")
}
}
// A button is wired to this; its purpose is to set off the
// KVO notifications for foo
#IBAction func changeFoo(_: Any?) {
self.foo = "Bar"
}
}
In addition, the following setup is done in the storyboard:
Add a tree controller, and connect the view controller's treeController outlet to it.
Bind the tree controller's "Content Array" binding to thingies on the view controller.
Set the tree controller's "Children Key Path" to children.
Create an outline view, and bind its "Content" and "Selection Index Paths" bindings to arrangedObjects and selectionIndexPaths respectively on the tree controller.
Create a button, and point it at the view controller's changeFoo: method.
If you'd like to try it yourself, I've uploaded a sample project here.
The behavior is as follows:
The notification for observer2 is always fired whenever the outline view's (and thus the tree controller's) selection changes, as one would expect.
However, the notification for observer1 is not fired when the outline view's selection changes.
However, observer1's notification is fired when the button is clicked, and foo is changed. This suggests that the property's dependencies are being considered, but just not for this one particular key path.
Using the old-school method with an observeValue(forKeyPath:bla:bla:bla:) override instead of the swank Swift 4 closure-based system seems to behave the same way.
EDIT: Well, it's not Swift's fault! Same thing happens when I write this program in Objective-C:
#interface Thingy: NSObject
#property (nonatomic, copy) NSString *name;
- (instancetype)initWithName:(NSString *)name;
#end
#implementation Thingy
- (instancetype)initWithName:(NSString *)name {
self = [super init];
if (self == nil) {
return nil;
}
self->_name = name;
return self;
}
- (NSArray *)children { return #[]; }
#end
void *ctxt1 = &ctxt1;
void *ctxt2 = &ctxt2;
#interface ViewController()
#property (nonatomic, strong) IBOutlet NSTreeController *treeController;
#property (nonatomic, copy) NSString *foo;
#property (nonatomic, copy) NSArray *thingies;
#end
#implementation ViewController
+ (NSSet *)keyPathsForValuesAffectingSelectionIndexPaths {
return [NSSet setWithObjects:#"treeController.selectionIndexPaths", #"foo", nil];
}
- (NSArray *)selectionIndexPaths {
return self.treeController.selectionIndexPaths;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.thingies = #[[[Thingy alloc] initWithName:#"Foo"], [[Thingy alloc] initWithName:#"Bar"]];
[self addObserver:self forKeyPath:#"selectionIndexPaths" options:0 context:ctxt1];
[self addObserver:self forKeyPath:#"treeController.selectionIndexPaths" options:0 context:ctxt2];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
if (context == ctxt1) {
NSLog(#"This only gets logged when I click the button");
} else if (context == ctxt2) {
NSLog(#"This gets logged whenever the selection changes");
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
- (IBAction)changeFoo:(__unused id)sender {
self.foo = #"Bar";
}
#end
I've been staring at this for a while, and I cannot figure out why directly observing treeController.selectionIndexPaths works, but observing a property that depends on treeController.selectionIndexPaths does not. And since I've generally felt like I had a pretty good handle on KVO and its workings, it is really bugging me that I can't explain this.
Does anyone know the reason for this discrepancy?
Thanks!

Related

Method is expected to return an instance of its class type

I have an NSTimer which is called from a class method to control an NSProgressBar. It works seemingly fine on the surface, although I get a warning as I'm using the NSTimer where I'm assuming the compiler wants the class name.
When I put the class name MyProgressBar in place of NSTimer the warning seems to go away. The reality of the matter is that all hell is breaking loose behind the scenes and the memory allocations start spiking up and up.
The question is, how should this really be done?
.h
#interface MyProgressBar : NSProgressIndicator {
double progressOffset;
NSTimer* animated;
}
#property (readwrite, retain) NSTimer* animated;
#property (readwrite) double progressOffset;
.m
- (void)setDoubleValue:(double)value {
[super setDoubleValue:value];
if (![self isDisplayedWhenStopped] && value == [self maxValue]) {
[self stopAnimation:self];
}
}
- (NSTimer*)animated { // This is the line with the warning
return animated; // using MyProgressBar ends up creating a memory leak
}
- (void)setAnimated :(NSTimer *)value {
if (animated != value) {
[animated invalidate];
animated = value;
}
}
- (id)initWithFrame :(NSRect)frameRect {
self = [super initWithFrame:frameRect];
if (self) {
self.progressOffset = 0;
self.animated = nil;
}
return self;
}
warning:
Method is expected to return an instance of its class type 'MyProgressBar', but is declared to return 'NSTimer *'
Overridden method returns an instance of its class type
-> complete github project here that displays the warning.
The getter should be called animated, not animate.
Also you should use underscore in all your instance variables (the default pattern) and drop the getter/setter as the compiler will provider better implementations (i.e. that setAnimated doesn't look right, if I recollect the use of manual reference counting). The getter should be returning an autoreleased object.

How to add observer on NSMutableArray?

I have searched a lot but didn't find useful code or tutorial.
In my application, I have an mutable array which update in every 60 seconds.
The objects in array is being displayed by table view in multiple view controllers.
I want to reload table view automatically when only when values in array changes or updated.
For this, I want to add observer on mutable array i.e when values in array changes then it should call a particular method for e.g
-(void)ArrayUpdatedNotification:(NSMutableArray*)array
{
//Reload table or do something
}
Thanks in advance.
You can abstract the array into a data container class with accessor methods, and then use key-value observing to observe when the array that backs the container object is changed (you cannot use KVO on an NSArray directly).
A simple example of a class used as an abstraction on top of an array follows. You use its insertObject:inDataAtIndex: and removeObjectFromDataAtIndex: methods instead of directly accessing the with addObject: and removeObject:.
// DataContainer.h
#interface DataContainer : NSObject
// Convenience accessor
- (NSArray *)currentData;
// For KVC compliance, publicly declared for readability
- (void)insertObject:(id)object inDataAtIndex:(NSUInteger)index;
- (void)removeObjectFromDataAtIndex:(NSUInteger)index;
- (id)objectInDataAtIndex:(NSUInteger)index;
- (NSArray *)dataAtIndexes:(NSIndexSet *)indexes;
- (NSUInteger)countOfData;
#end
// DataContainer.m
#interface DataContainer ()
#property (nonatomic, strong) NSMutableArray *data;
#end
#implementation DataContainer
// We'll use automatic notifications for this example
+ (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key
{
if ([key isEqualToString:#"data"]) {
return YES;
}
return [super automaticallyNotifiesObserversForKey:key];
}
- (id)init
{
self = [super init];
if (self) {
// This is the ivar which provides storage
_data = [NSMutableArray array];
}
return self;
}
// Just a convenience method
- (NSArray *)currentData
{
return [self dataAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [self countOfData])]];
}
// These methods enable KVC compliance
- (void)insertObject:(id)object inDataAtIndex:(NSUInteger)index
{
self.data[index] = object;
}
- (void)removeObjectFromDataAtIndex:(NSUInteger)index
{
[self.data removeObjectAtIndex:index];
}
- (id)objectInDataAtIndex:(NSUInteger)index
{
return self.data[index];
}
- (NSArray *)dataAtIndexes:(NSIndexSet *)indexes
{
return [self.data objectsAtIndexes:indexes];
}
- (NSUInteger)countOfData
{
return [self.data count];
}
#end
The reason that we do this is so we can now observe changes made to the underlying array. This is done through Key Value Observing. A simple view controller that instantiates and observes a data controller is shown:
// ViewController.h
#interface ViewController : UIViewController
#end
// ViewController.m
#interface ViewController ()
#property (nonatomic,strong) DataContainer *dataContainer;
#end
#implementation ViewController
static char MyObservationContext;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Instantiate a DataContainer and store it in our property
_dataContainer = [[DataContainer alloc] init];
// Add self as an observer. The context is used to verify that code from this class (and not its superclass) started observing.
[_dataContainer addObserver:self
forKeyPath:#"data"
options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew)
context:&MyObservationContext];
}
return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
// Check if our class, rather than superclass or someone else, added as observer
if (context == &MyObservationContext) {
// Check that the key path is what we want
if ([keyPath isEqualToString:#"data"]) {
// Verify we're observing the correct object
if (object == self.dataContainer) {
NSLog(#"KVO for our container property, change dictionary is %#", change);
}
}
}
else {
// Otherwise, call up to superclass implementation
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Insert and remove some objects. Console messages should be logged.
[self.dataContainer insertObject:[NSObject new] inDataAtIndex:0];
[self.dataContainer insertObject:[NSObject new] inDataAtIndex:1];
[self.dataContainer removeObjectFromDataAtIndex:0];
}
- (void)dealloc
{
[_dataContainer removeObserver:self forKeyPath:#"data" context:&MyObservationContext];
}
#end
When this code runs, three changes to the data are observed by the view controller and logged to the console:
KVO for our container property, change dictionary is {
indexes = "<NSIndexSet: 0x8557d40>[number of indexes: 1 (in 1 ranges), indexes: (0)]";
kind = 2;
new = (
"<NSObject: 0x8557d10>"
);
}
KVO for our container property, change dictionary is {
indexes = "<NSIndexSet: 0x715d2b0>[number of indexes: 1 (in 1 ranges), indexes: (1)]";
kind = 2;
new = (
"<NSObject: 0x71900c0>"
);
}
KVO for our container property, change dictionary is {
indexes = "<NSIndexSet: 0x8557d40>[number of indexes: 1 (in 1 ranges), indexes: (0)]";
kind = 3;
old = (
"<NSObject: 0x8557d10>"
);
}
While this is somewhat complex (and can get much more involved), this is the only way to be notified automatically that a mutable array's contents were changed.
What is can do is - After updating your Array send a Notification (NSNotificationCenter) and this notification will be received by all the controllers. On receiving the notificaiton the controller should do [tableview reloaddata].
Code example:
// Adding an observer
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(updateTable:) name:#"arrayUpdated" object:nil];
// Post a notification
[[NSNotificationCenter defaultCenter] postNotificationName:#"arrayUpdated" object:nil];
// the void function, specified in the same class where the Notification addObserver method has defined
- (void)updateTable:(NSNotification *)note {
[tableView reloadData];
}
If you want to use shiny blocks you can do this
// Create an instance variable for your block holder in your interface extension
#property (strong) id notificationHolder;
// Listen for notification events (In your TableView class.
self.notificationHolder = [[NSNotificationCenter defaultCenter] addObserverForName:#"NotificationName"
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note) {
NSLog(#"Received notification");
}];
Then in dealloc (or when you don't use it anymore)
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self.notificationHolder];
}
Then in some other class
// Send a notification
[[NSNotificationCenter defaultCenter] postNotificationName:#"NotificationName" object:nil];
Ask if something is not clear! Hope it helps!
EDIT DUE TO COMMENT
The "YourEvent" is the name of the notification, this means that you can name it to whatever you want. (Perhaps "UpdateArrayNotification could be a good name?)
Something to think about: Note that you can have several observers for the same notification. This means that one 'post' will be snapped up by all observers.

ControlTextDidChange not working for setting string of NSTextField

I'm trying to find a method that monitors the text of NSTextField for changes. I tried the delegate method of -(void)controlTextDidChange:(NSNotification *)obj but it only works when the user types into the text field. If the text field string is programmatically set, such as with a button, the controlTextDidChange doesn't work.
Is there a method or another approach that I can use to monitor the contents of a NSTextField for changes?
My ButtonText class (set as delegate for the NSTextField):
#import "ButtonText.h"
#interface ButtonText ()
#property (weak) IBOutlet NSTextField *buttonField;
#end
#implementation ButtonText
- (IBAction)buttonTextA:(id)sender {
[_buttonField setStringValue:#"text A here"];
}
- (IBAction)buttonTextB:(id)sender {
[_buttonField setStringValue:#"and text B stuff"];
}
- (void)controlTextDidChange:(NSNotification *)obj {
NSLog(#"controlTextDidChange: %#", _buttonField.stringValue);
}
#end
The XIB showing the buttons and text field:
One approach is to use KVO. In particular, add the ButtonText instance as an observer of buttonField's stringValue.
In more detail, in your file ButtonText, once the #property IBOutlet buttonField has been set (i.e. if ButtonText is an NSWindowController subclass, in -windowDidLoad, and if ButtonText is an NSViewController subclass in -loadView), call
[self.buttonField addObserver:self
forKeyPath:#"stringValue"
options:0
context:&ButtonTextKVOContext];
Define ButtonTextKVOContext previously in the file as follows:
static int ButtonTextKVOContext = 0;
Then override observeValueForKeyPath:ofObject:change:context: as follows:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (context != &ButtonTextKVOContext) {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
return;
}
if (object == self.buttonField) {
if ([keyPath isEqualToString:#"stringValue"]) {
NSLog(#"controlTextDidChange: %#", _buttonField.stringValue);
}
}
}
Edit
Since ButtonText is not a subclass of NSWindowController or NSViewController, we'll use a slightly different approach. As before, we'll want to start observing "once the #property IBOutlet buttonField has been set". To do this, synthesize the property buttonField to be the member variable mButtonField writing
#synthesize buttonField = mButtonField;
and override buttonField's setter as follows:
- (void)setButtonField:(NSTextField *)buttonField
{
[self stopObservingButtonField];
mButtonField = buttonField;
[self startObservingButtonField];
}
We need to make sure that ButtonText stops observing the button field when it deallocates as well, so override -dealloc as follows:
- (void)dealloc
{
[self stopObservingButtonField];
}
It remains to define the methods -stopObservingButtonField and -startObservingButtonField:
- (void)stopObservingButtonField
{
if (mButtonField) {
[mButtonField removeObserver:self
forKeyPath:#"stringValue"
context:&ButtonTextKVOContext];
}
}
- (void)startObservingButtonField
{
if (mButtonField) {
[self.buttonField addObserver:self
forKeyPath:#"stringValue"
options:0
context:&ButtonTextKVOContext];
}
}
As a result of this arrangement, we must never set the mButtonField variable outside of the -setButtonField: method. (This isn't quite true, but if we do set mButtonField we must be sure to first of all stop observing its old value's #"stringValue" key path and start observing its new value's #"stringValue" key path. Doing this rather than simply calling -setButtonField: would very likely simply constitute code repetition and not be worthwhile.)
For reference, check out Apple's documentation on the NSKeyValueObserving protocol.
If your goal is to use bindings, then you can override the setter method for the property you have bound to the text field's value, and do whatever monitoring you want to do there. So,for instance, you have a text field whose value is bound to the property, myText, then you could do something like this:
-(void)setMyText:(NSString *) newValue {
_myText= newValue;
// do monitoring here
}
This should be called any time the user either types in the text field or you change the value in code, as long as you do it through the property, and not by directly accessing the ivar.

Shared UITableViewDelegate

I'm writting a subclass of UITableView and I want my subclass to handle some of the UITableViewDelegate methods itself before passing them along to the "real" delegate as well as forward all the UITableViewDelegate methods not implemented by my subclass.
In the subclass I have a private property:
#property (nonatomic, assign) id <UITableViewDelegate> trueDelegate;
which holds the "real delegate" that all the unimplemented methods should forward to. In both my init methods I set
self.delegate = self;
and I override - (void)setDelegate:(id) like this
-(void)setDelegate:(id<UITableViewDelegate>)delegate {
if (delegate != self) {
_trueDelegate = delegate;
} else {
[super setDelegate:self];
}
}
Then I override these to handle the message forwarding
-(NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
NSMethodSignature *sig;
sig = [[self.delegate class] instanceMethodSignatureForSelector:aSelector];
if (sig == nil) {
sig = [NSMethodSignature signatureWithObjCTypes:"#^v^c"];
}
return sig;
}
- (void)forwardInvocation:(NSInvocation *)anInvocation {
SEL selector = anInvocation.selector;
if ([self respondsToSelector:selector]) {
[anInvocation invokeWithTarget:self];
} else {
[anInvocation invokeWithTarget:_trueDelegate];
}
}
The problem is that the unimplemented delegate methods never get called on the tableview, therefore they are not given a chance to be forwarded along to the _trueDelegate object.
I tried checking for them here:
- (BOOL)respondsToSelector:(SEL)aSelector {
}
but that method is never called for the UITableViewDelegate methods although it catches other methods just fine.
For performance, UITableView checks and remembers which delegate methods are available as soon as the delegate is set. You set the delegate self first, then the trueDelegate. So at the time the delegate is set on the UITableView, trueDelegate is nil, and so -respondsToSelector: on that one always returns NO.
To fix that, set the delegate after trueDelegate is set. Also, you can simplify the forwarding code. Remove all the code you have above except for the property and replace it with:
- (void)setDelegate:(id <UITableViewDelegate>)delegate
{
if (delegate == self) return;
self.trueDelegate = delegate;
[super setDelegate:self];
}
- (BOOL)respondsToSelector:(SEL)aSelector
{
if ([super respondsToSelector:aSelector]) return YES;
return [self.trueDelegate respondsToSelector:aSelector];
}
- (id)forwardingTargetForSelector:(SEL)aSelector
{
return self.trueDelegate;
}

How to change all my application texts (UILabel, UITableCell.....) color?

I would like to do something similar in CSS than "BODY {color:red;}" but in objectif C. I mean if I have 10 differents UIView, I would like to change all the UIView texts color in one time.
Cheers
Simple case - you somehow gathered them together
for (UIView *v in styledViews) {
// apply current style here
}
I doubt that this is your case
Complex case - there are tons of styled views everywhere.
Disclaimer: I can't guarantee anything about following code, it works on my simulator, which doesn't mean it will not blow up in user's hands. I wrote it because it was fun and may help Thomas to solve his problem. I didn't check documentation thoroughly because it's already 5 a.m. here
1) Encapsulate style stuff in some StyleManager class (in this example applyCurrentStyle: will apply current style to any view passed to it). It should post notification each time style is changed (e.g. kStyleManagerNotificationStyleChanged)
2) Make UIView category (like UIView+Style) with public setStyleManager: method.
3) Implement it:
#import "UIView+Style.h"
#import <objc/runtime.h>
#interface StyleSubscription : NSObject {
StyleManager *styleManager;
NSObject *subscriber;
}
#property (readonly) StyleManager *styleManager;
- (id)initWithStyleManager:(NSObject*)p subscriber:(NSObject*)s;
#end
#implementation StyleSubscription
#synthesize styleManager;
- (id)initWithStyleManager:(StyleManager*)sManager subscriber:(NSObject*)s {
if (self = [super init]) {
styleManager = [sManager retain];
subscriber = s;
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:subscriber
name:kStyleManagerNotificationStyleChanged
object:styleManager];
[styleManager release];
[super dealloc];
}
#end
#implementation UIView (Style)
static char styleSubsriptionKey;
- (StyleManager*)styleManager {
StyleSubscription *s = objc_getAssociatedObject(self, &styleSubsriptionKey);
return s.styleManager;
}
- (void)styleChanged:(NSNotification*)n {
[[self styleManager] applyCurrentStyle:self];
}
- (void)setStyleManager:(StyleManager*)sManager {
if ([self styleManager] == sManager) {
return;
}
StyleSubscription *subscr = nil;
if (sManager != nil) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(styleChanged:)
name:kStyleManagerNotificationStyleChanged
object:sManager];
subscr = [[[StyleSubscription alloc] initWithStyleManager:sManager
subscriber:self] autorelease];
}
objc_setAssociatedObject(self, &styleSubsriptionKey, subscr, OBJC_ASSOCIATION_RETAIN);
[sManager applyCurrentStyle:self];
}
#end
Each time style manager posts notification correspondent views will be updated with a new style. View will unsubscribe from style notifications automatically upon deallocation. Style manager can be removed explicitly [view setStyleManager:nil].
you can fix this in a nice way.here is a tutorial
http://dot-ios.blogspot.com/2013/02/design-uilabel-in-optimize-way-for.html