NSCell -stringValue calls -setStringValue?

In essence, here is my code in a subclass of NSTextFieldCell:

- (void)setStringValue:(NSString *)aString {
    [super setStringValue:aString];
    [self doSomething];

- (void)doSomething {
    NSLog(@"%@", [self stringValue]);

In essence, I'm trying to get my subclass notified of a change in the string value, and when the value changes, I want the subclass of NSCell to be able to do something with that new value (Using -attributedStringValue would be even better, because I want to cache it for special drawing). The problem is, for some reason, calling -(NSString *)stringValue somehow results in a call to -(void)setStringValue which ends up becoming...

...an infinite loop. Can someone enlighten me on this - and a possible workaround?


Probably stringValue is doing some lazy initialization. If it detects that it has no stringValue but can derive the value from something else (perhaps attributedStringValue?), then it derives the value, calls [self setStringValue:derivedValue], then returns the value.

This does make life tricky, though. Try something like this?

@implementation MyCell ()
    BOOL gettingStringValue;

- (NSString*)stringValue {
    gettingStringValue = YES;
    NSString* stringValue = [super stringValue];
    gettingStringValue = NO;
    return stringValue; 

- (void)setStringValue:(NSString *)aString {
    [super setStringValue:aString];
    if (!gettingStringValue)
        [self doSomething];

Need Your Help

how to show data in labels in c#

c# sql .net labels data-layer

I have two layers, a data layer which gets the results from the database and a UI layer which will show the data in label controls

jquery find div or select by name returning 0 results

javascript jquery html jquery-selectors

I'm trying to select a <div> inside of a <p> by name, but it's returning 0 results. The same type of selection works fine for <input> and <span>, but not for <select>...