Skip navigation.
 
mlSimple bindings problem
FROM : Martin Linklater
DATE : Sat Jan 05 10:58:32 2008

I'm writing some very simple applications to lear how bindings work 
and I've hit upon another problem that I can't figure out. Hopefully 
it's not some dismal typo on my part 8).

I have a simple application with two NSTextViews - one for displaying 
an integer value and one for displaying an NSString value. My 
AppController has one int member and one NSString member (code below). 
I have an NSObjectController which has it's content bound to my 
AppController. I have bound both of the NSTextViews to my Controller 
and set their Controller Key to 'selection' and their Model Key Path 
to the names of my AppController member variables.

The problem I am having is that my integer binding is working fine - 
updates from both the model and the view are propigated by the 
binding. But my NSString member is causing a runtime crash:

2008-01-05 09:45:19.188 BindingsTest[9890:813] *** -[NSTextField 
copyWithZone:]: unrecognized selector sent to instance 0x12bc50
2008-01-05 09:45:19.189 BindingsTest[9890:813] An uncaught exception 
was raised
2008-01-05 09:45:19.190 BindingsTest[9890:813] *** -[NSTextField 
copyWithZone:]: unrecognized selector sent to instance 0x12bc50
2008-01-05 09:45:19.190 BindingsTest[9890:813] *** Terminating app due 
to uncaught exception 'NSInvalidArgumentException', reason: '*** -
[NSTextField copyWithZone:]: unrecognized selector sent to instance 
0x12bc50'

Yet both of the bindings are set up the same in IB...

When I put breakpoints on the setter and getter methods for the 
NSString my code seems to be initialising my AppController fine and 
setting the member variables, but then immediately tries to access a 
different instance for some reason (the 'self' ptr changes). This is 
when the crash happens. Problem is that I have no idea why my code is 
setting up a correct instance of my AppController and then trying to 
read the contents of an invalid instance of my AppController class. 
Can anyone help ? Thank you.

Here is the code for my AppController:

---- header

#import <Cocoa/Cocoa.h>

@interface AppController : NSObject {
   int    testInt;
   NSString* insertText;
}

- (void)setTestInt:(int)intIn;
- (int)testInt;

- (NSString*)insertText;
- (void)setInsertText:(NSString*)insertIn;

@end

---- implimentation

#import "AppController.h"

@implementation AppController

- (id)init
{
   if( self = [super init] )
   {
       [self setTestInt:10];
       [self setInsertText:@"five"];
   }
   return self;
}

- (void)setTestInt:(int)intIn
{
   NSLog( @"value set to %d", intIn);
   testInt = intIn;
}

- (int)testInt
{
   NSLog(@"testInt read");
   return testInt;
}

- (NSString*)insertText
{
   NSLog(@"reading insertText");
   return insertText;
}

- (void)setInsertText:(NSString*)insertIn
{
   NSLog(@"setting insertText");
   if( insertText != insertIn )
   {
       [insertText release];
       insertText = insertIn;
       [insertText retain];
   }
}

@end

Related mailsAuthorDate
mlSimple bindings problem Martin Linklater Jan 5, 10:58
mlRe: Simple bindings problem Martin Linklater Jan 5, 12:46
mlRe: Simple bindings problem Martin Linklater Jan 5, 23:02
mlRe: Simple bindings problem mmalc crawford Jan 6, 01:24
mlRe: Simple bindings problem Martin Linklater Jan 6, 09:22
mlRe: Simple bindings problem mmalc crawford Jan 6, 10:58
mlRe: Simple bindings problem Martin Linklater Jan 6, 14:13