Clearing NSTextfield bound to a nsmutablestring

301 Views Asked by At

I have a text field bound to a nsmutableablestring. In the action item for a button I want to clear this string but it's throwing an exception saying it's immutable.

Member variable is

NSMutableString* firstName;

Property for binding to the text field is declared in .h

@property (copy) NSMutableString* firstName;

In the action for the button the following line throws an exception

[firstName setString:@""];

I don't have any trouble reading the value in firstName to access what is in the text field so the binding appears to be working.

Here is the stack trace

Attempt to mutate immutable object with setString:
2012-01-22 14:48:58.084 testproj[2509:707] (
    0   CoreFoundation                      0x00007fff88233286 __exceptionPreprocess + 198
    1   libobjc.A.dylib                     0x00007fff857bdd5e objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff882330ba +[NSException raise:format:arguments:] + 106
    3   CoreFoundation                      0x00007fff88233044 +[NSException raise:format:] + 116
    4   CoreFoundation                      0x00007fff882892dd mutateError + 93
    5   testproj                            0x00000001000035c4 -[AppController AddCustomer:] + 388
    6   CoreFoundation                      0x00007fff88222a1d -[NSObject performSelector:withObject:] + 61
    7   AppKit                              0x00007fff8f319710 -[NSApplication sendAction:to:from:] + 139
    8   AppKit                              0x00007fff8f319642 -[NSControl sendAction:to:] + 88
    9   AppKit                              0x00007fff8f31956d -[NSCell _sendActionFrom:] + 137
    10  AppKit                              0x00007fff8f318a30 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2014
    11  AppKit                              0x00007fff8f3988e0 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 489
    12  AppKit                              0x00007fff8f31763a -[NSControl mouseDown:] + 786
    13  AppKit                              0x00007fff8f2e20e0 -[NSWindow sendEvent:] + 6306
    14  AppKit                              0x00007fff8f27a68f -[NSApplication sendEvent:] + 5593
    15  AppKit                              0x00007fff8f210682 -[NSApplication run] + 555
    16  AppKit                              0x00007fff8f48f80c NSApplicationMain + 867
1

There are 1 best solutions below

0
On BEST ANSWER

Are you relying on the synthesized setter? copy returns an immutable copy so you need to define your own setter. See http://vgable.com/blog/2009/03/17/mutable-property-and-copy-gotcha/ for more details