I am trying to observe value changes of an NSMutableArray
in an object written in ObjectiveC. The observer however is an UIViewController
written in Swift.
I'm not able to get the KVO notifications. These are my changes:
@interface Record : BaseObject
@property (nonatomic) NSMutableArray *commentsArray;
-(NSUInteger) countOfCommentsArray {
return _commentsArray.count;
}
-(id) objectInCommentsArrayAtIndex:(NSUInteger)index {
return [_commentsArray objectAtIndex:index];
}
-(void) insertObject:(Comment *)comment inCommentsAtIndex:(NSUInteger)index {
[_commentsArray insertObject:comment atIndex:index];
}
-(void) insertComments:(NSArray *)array atIndexes:(NSIndexSet*)indexes {
[_commentsArray insertObjects:array atIndexes:indexes];
}
-(void) removeCommentsAtIndexes:(NSIndexSet *)indexes {
[_commentsArray removeObjectsAtIndexes:indexes];
}
-(void) removeObjectFromCommentsAtIndex:(NSUInteger)index {
[_commentsArray removeObjectAtIndex:index];
}
-(void) replaceObjectInCommentsAtIndex:(NSUInteger)index withObject:(id)object {
[_commentsArray replaceObjectAtIndex:index withObject:object];
}
The BaseObject derives from NSObject
.
In my Observer class which is a Viewcontroller, and written in Swift I have the following.
In one of setup methods.
myInspectionRecord?.addObserver(self, forKeyPath: "commentsArray", options: NSKeyValueObservingOptions.New, context: nil);
And I have the method overrriden.
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
print("Value Changed");
}
But somehow when I add a new object to the array using the API itself, the observeValue
method doesnt seem to be triggered.
Any pointers on what I might be missing here?
Or doesn't KVO work between Swift and Objective C files?
Any help would be appreciated. Thanks!