开发者

Null selector sent to first item in NSMutableArray

开发者 https://www.devze.com 2022-12-10 06:53 出处:网络
Whenever I call this method: - (IBAction)addListItem: (id)sender { NSString *newItemText = [inputField stringValue];

Whenever I call this method:

- (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: @""];  
}

the first TodoItem instance in list is sent a null selector, and the console prints an error. It happens twice, and doesn't happen more when I call addObject more than once, or call didChangeValueForKey multiple times. Any ideas how I can stop this from happening?

The error message that is displayed is:

-[TodoItem <null selector>]: unrecognized selector sent to instance 0x11470b0e0

here's the stack backtrace when I set a breakpoint in the [NSObject(NSObject) doesNotRecognizeSelector] method:

#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 _invokeMultipleS开发者_如何学JAVAelector: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


It looks like you have a control whose target property is bound to the todo item, but without an action binding. If that’s the case, you should use a regular IB connection to set up the action instead.


(1) It is difficult to answer the question without knowing more about the TodoItem class. How does that initializer work, for example?

(2) An unrecognized selector typically leads to a thrown exception. Set a breakpoint on objc_exception_throw. The backtrace should tell you (and us) considerably more about the context of the error.

0

精彩评论

暂无评论...
验证码 换一张
取 消