1

每当我调用此方法时:

- (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
4

2 回答 2

1

TodoItem(1) 不知道更多关于课堂的知识很难回答这个问题。例如,该初始化程序是如何工作的?

(2) 无法识别的选择器通常会导致抛出异常。在 objc_exception_throw 上设置断点。回溯应该告诉你(和我们)更多关于错误上下文的信息。

于 2009-11-07T09:36:15.053 回答
1

看起来您有一个控件,其目标属性绑定到待办事项项,但没有操作绑定。如果是这种情况,您应该使用常规的 IB 连接来设置操作。

于 2009-11-11T15:09:40.463 回答