Hi! I’m new To MacRuby, all day I was trying to find what causes the
error in my Code.
I have a ‘Scene’ class with an attribute ‘nodes’, which is an array. I
have set up kvo methodes for the array
I have an Observer object, who observes the scene.nodes property. The
observer’s options is
‘NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld’
when I add a node to the scene with scene.addNode( new object )
everything is working.
but when I try to remove a node I got an error:
NSInvalidArgumentException: -[NSCFArray countForObject:]: unrecognized
selector sent to instance 0x1b4c210
I only got this error, when I have the NYKeyValueObserbingOptionOld. But
I would really need it.
I assume, the somewhere MacRuby switch the class of the supplied NSSet
containing the objects involved in the change (in the manual KVO call:
willChangeValueForKey: … withSetMutation… objects:(NSSet) )
please help!
Thanks
here is the appropiate code:
OBSERVER.rb
class ConnectionViewController
attr_accessor:scene,:selection
attr_accessor:view
def initialize
puts ‘ConnectionViewController=>initialize’
NSBundle.loadNibNamed(“ConnectionViewController”, owner:self)
addObserver(self,
forKeyPath:‘scene.nodes’,
options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld,
context:nil)
node=Node.new
scene.addNode(node)
scene.removeNode(node)
end
def observeValueForKeyPath(keyPath,
ofObject:object,
change:change,
context:context)
puts self,’ observed:’,change,’ keyPath:’,keyPath
case change[‘kind’]
when NSKeyValueChangeInsertion then
puts 'insertion: '+change[‘new’].allObjects.to_s
when NSKeyValueChangeRemoval then
puts 'removal: '+change['old'].allObjects.to_s
end
end
end
SCENE.m
#import “Scene.h”
@implementation Scene
@synthesize title;
@synthesize nodes,wires;
- (id) init
{
self = [super init];
if (self != nil) {
title=@“untitled scene”;
nodes=[NSMutableArray new];
wires=[NSMutableArray new];
NSLog(@"%@=>init",self);
}
return self;
}
//PROPERTY METHODES
#pragma mark PROPERTY METHODES
-(Node*)addNode:(Node*)node{
NSLog(@“Scene->addNode:%@”,node);
NSSet nodeSet=[NSSet setWithObject:node];
[self willChangeValueForKey:@“nodes”
withSetMutation:NSKeyValueUnionSetMutation usingObjects:nodeSet];
[nodes addObject:node];
node.scene=self;
[self didChangeValueForKey:@“nodes”
withSetMutation:NSKeyValueUnionSetMutation usingObjects:nodeSet];
return node;
}
-(void)removeNode:(Node)node{
NSSet *nodeSet=[NSSet setWithObjects:node,nil];
NSLog(@“Scene->removeNode %@”,node);
[self willChangeValueForKey:@“nodes”
withSetMutation:NSKeyValueMinusSetMutation usingObjects:nodeSet];
[nodes removeObject:node];
[self didChangeValueForKey:@“nodes”
withSetMutation:NSKeyValueMinusSetMutation usingObjects:nodeSet];
}
@end