17

First of all I have seen that there are many questions about "unrecognized selector sent to instance" issue.
I have seen few but saw nothing about accessing a defined in category property...

I have a category on UILabel with a property.
The getter and the setter are defined.
Actually I have the same property in 2 different categories (for 2 different classes: UIButton and UILabel).
The problem is that I can access this property for UIButton but not for UILabel.
Once I try to access any method/property in UILabel(text) category it drops the "-[UILabel test]: unrecognized selector sent to instance 0x4e539f0" exception.

Both categories files are imported.

I have no idea what is the problem.

Here is some code:

// UILabel+text.h
@interface UILabel (text)
  - (void)test;
@end

// UILabel+text.m
@implementation UILabel (text)
- (void)test {
  NSLog(@"test");
}
@end

// UIButton+text.h
@interface UIButton (text)
  - (void)test;
@end

// UIButton+text.m
@implementation UIButton (text)
- (void)test {
  NSLog(@"test");// works   
}
@end

// Usage (in UIViewController class) - both elements are defined in XIB
[self.button test];// works
[self.label test];// exception

Any help will be appreciated.
I don't have a clue for possible problem...

Thank you.

Michael.

Michael Kessler
  • 14,245
  • 13
  • 50
  • 64

2 Answers2

42

Are you using a static library? If so, add all_load to Other Linker Flags.

Are you sure "UILabel+text.m" is in the target?

Steven Kramer
  • 8,473
  • 2
  • 37
  • 43
  • 4
    That's it! I always forget the targets issue... Is there a way to define the new files to be added to all the targets by default? – Michael Kessler Mar 03 '11 at 11:44
  • That I don't know, Xcode usually seems sense its way in my case. Maybe it defaults to your last selection? – Steven Kramer Mar 03 '11 at 22:18
  • `-all_load` works most of the time, if you want to know the reason then check this answer: http://stackoverflow.com/a/2906210/146032 – bithavoc Jan 13 '13 at 16:41
  • 1
    "all_load" is NOT needed any more (Apple finally fixed that) - but Xcode 4.0 -> 4.6 (and more, maybe) has a nasty bug where categories sometimes disappear from the list of "compile sources", or their headers disappear from the list of "copy headers" in a library build. Thanks! – Adam Apr 25 '13 at 14:21
  • I am on Xcode 5.1.1 and I need to add `-all_load`to Other Linker Flags. – WebOrCode Aug 29 '14 at 16:56
0

I experienced the same issue in a project with multiple static frameworks.

Adding all_load to Linker Flags did not solve the issue for me! I had to enable the Build Setting GENERATE_MASTER_OBJECT_FILE = YES so that the category was properly found during runtime.

mrtnlst
  • 173
  • 7