0

I am trying to subclass UILabel so the setter setText check for null to avoid this if the supplied parameter is a NSNULL: [NSNull isEqualToString:]: unrecognized selector sent to instance

The code is as below. I stepped through in a debugger and it didn't go into the new setter which I called by:

    [someSubClassLabel setText:someValueWhichMayBeNull];

where someSubClassLabel is a UILabelWithNULLHandler.

in the .h

#import <UIKit/UIKit.h>
@interface UILabelWithNULLHandler : UILabel
- (void) setText:(NSString *) newText;
@end

Defined the method in the .m:

- (void) setText:(NSString *) newText
{
    if(![newText isKindOfClass:[NSNull class]])
    {
        super.text = newText;
    }
    else
    {
        super.text = @"";
    }
}

Edit: I suppose I can add code to handle nil, but for the time being, I am dealing with NSNUll which I pulled from a NSDictionary.

NaN
  • 598
  • 3
  • 15
  • Are you sure isKindOfClass is necessary? Why not: if(newText){ // Do sth ... – Yunus Nedim Mehel Feb 25 '13 at 11:54
  • 3
    And `someSubClassLabel` is an instance of `UILabelWithNULLHandler`? – trojanfoe Feb 25 '13 at 11:55
  • 1
    Are you sure the type of of `someSubClassLabel` is your new `UILabelWithNULLHandler`? – Adam Wright Feb 25 '13 at 11:55
  • Since you are overriding a method I think you don't need to define it again in the header file. But I don't know if that changes anything. – Odrakir Feb 25 '13 at 12:01
  • My money's on forgetting to make the label use the new class. If you're using a NIB you need to edit the nib to change the class of the label. Simply changing the class of the property is not sufficient. – Hot Licks Feb 25 '13 at 12:24
  • Doesn't address the problem, but you can simplify the test to `newText != [NSNull null]`. – Hot Licks Feb 25 '13 at 12:29
  • @HotLicks Thanks. I am using storyboard though, but will see if that also need changing like a NIB. – NaN Feb 25 '13 at 12:53
  • I've never mucked with storyboards, but I'm reasonably certain that would need changing too. – Hot Licks Feb 25 '13 at 13:22
  • Yeah, the storyboard is definitely just like a NIB in that regard (how else would it know what class you want the label to be?) A quick test of your null test show it should work. You could also consider a class extension to NSDictionary and make a objectForKeyNotNull method that returns nil or @"" instead of the NSNull. – Ben Flynn Feb 25 '13 at 14:35

1 Answers1

0

@Hot Licks was right. I still have a UILabel in my storyboard. Need to set the new class name in the Identity Inspector under Custom Class.

NaN
  • 598
  • 3
  • 15