空选择器发送到NSMutableArray中的第一个项目

时间:2009-11-07 03:47:25

标签: cocoa nsmutablearray

每当我打电话给这个方法时:

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

2 个答案:

答案 0 :(得分:1)

(1)如果不了解TodoItem课程,很难回答这个问题。例如,初始化程序如何工作?

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

答案 1 :(得分:1)

看起来你有一个控件,其target属性绑定到todo项,但没有动作绑定。如果是这种情况,则应使用常规IB连接来设置操作。