每当我调用此方法时:
- (IBAction)addListItem: (id)sender {
NSString *newItemText = [inputField stringValue];
TodoItem *newItem = [[TodoItem alloc] initWithGUID:nil text:newItemText];
[newItem autorelease];
[self willChangeValueForKey: @"list"];
[list addObject: newItem];
[self didChangeValueForKey: @"list"];
[inputField setStringValue: @""];
}
第一个TodoItem
实例list
被发送一个空选择器,控制台打印一个错误。addObject
它发生了两次,当我多次调用或didChangeValueForKey
多次调用时不会发生更多。有什么想法可以阻止这种情况发生吗?
显示的错误消息是:
-[TodoItem <null selector>]: unrecognized selector sent to instance 0x11470b0e0
这是我在方法中设置断点时的堆栈回溯[NSObject(NSObject) doesNotRecognizeSelector]
:
#0 0x7fff87ea32fa in objc_exception_throw
#1 0x7fff86c952a0 in -[NSObject(NSObject) doesNotRecognizeSelector:]
#2 0x7fff86c0f30f in ___forwarding___
#3 0x7fff86c0b458 in __forwarding_prep_0___
#4 0x7fff831d099b in -[NSObject(_NSBinderKeyValueCodingAdditions) _invokeSelector:withArguments:onKeyPath:]
#5 0x7fff82e4b171 in -[NSArrayController _invokeMultipleSelector:withArguments:onKeyPath:atIndex:]
#6 0x7fff82e4a423 in -[NSArrayController _invokeSingleSelector:withArguments:onKeyPath:]
#7 0x7fff831d08d1 in -[NSObject(_NSBinderKeyValueCodingAdditions) _invokeSelector:withArguments:onKeyPath:]
#8 0x7fff82e5b4d0 in -[NSBinder _invokeSelector:withArguments:onKeyPath:ofObject:mode:raisesForNotApplicableKeys:]
#9 0x7fff82e57f3b in -[NSBinder invokeSelector:withArguments:forBinding:error:]
#10 0x7fff82e3a0ba in -[NSActionBinder _invokeSelector:withArguments:forBinding:]
#11 0x7fff82e39f20 in -[NSActionBinder _ performActionWithCommitEditing:didCommit:contextInfo:]
#12 0x7fff82daa259 in _NSSendCommitEditingSelector
#13 0x7fff82dabbc7 in -[NSController _controllerEditor:didCommit:contextInfo:]
#14 0x7fff86c1235c in __invoking___
#15 0x7fff86c1222d in -[NSInvocation invoke]
#16 0x7fff86c2df21 in -[NSInvocation invokeWithTarget:]
#17 0x7fff8363d500 in __NSFireDelayedPerform
#18 0x7fff86bd9a78 in __CFRunLoopRun
#19 0x7fff86bd803f in CFRunLoopRunSpecific
#20 0x7fff84c4bc4e in RunCurrentEventLoopInMode
#21 0x7fff84c4b9b1 in ReceiveNextEventCommon
#22 0x7fff84c4b90c in BlockUntilNextEventMatchingListInMode
#23 0x7fff82be8520 in _DPSNextEvent
#24 0x7fff82be7e89 in -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:]
#25 0x7fff82bada7d in -[NSApplication run]
#26 0x7fff82ba6798 in NSApplicationMain
#27 0x100001211 in main at main.m:13