1

Well, this is quite a weird issue. (I just hope it has something to do with my not-playing-that-much-with-Cocoa-for-a-while, or else...)

So, the issue is quite straightforward :

  • I'm using Xcode 4.3.3 (a very simple test project - 10.7 SDK - no ARC)
  • I'm creating a Category on some Class (e.g. NSProgressIndicator)
  • I'm including the appropriate header file
  • When trying to use any of my Category's methods (however, it still shows up in the dropdown of available commands), I'm getting an error :

[NSProgressIndicator start]: unrecognized selector sent to instance 0x7f9f4b91a0a0

The code

(as an example - it has happened with other (100-times tested) categories):
#import <Foundation/Foundation.h>


@interface NSProgressIndicator (NSProgressIndicator_Functions)

- (void)start;
- (void)stop;

@end

#import "NSProgressIndicator+Functions.h"


@implementation NSProgressIndicator (NSProgressIndicator_Functions)

- (void)start
{
    [self setHidden:NO];
    [self startAnimation:nil];
}

- (void)stop
{
    [self setHidden:YES];
    [self stopAnimation:nil];
}

@end

Any ideas?

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
  • 3
    Is the category's implementation (.m) file included in your target's Compile Sources build phase? That'd be the first thing I'd check. – Andrew Madsen Aug 26 '12 at 14:46
  • @AndrewMadsen UPDATE: Nope, actually it wasn't! OMG, I feel like stupid. Andrew, please post that as a proper answer (so that others take notice too), and I'll accept it. Thanks a lot! – Dr.Kameleon Aug 26 '12 at 14:52
  • This is the same issue as: [Objective-C Category Causing unrecognized selector][1] [1]: http://stackoverflow.com/questions/3998483/objective-c-category-causing-unrecognized-selector – Jimmy Luong Aug 26 '12 at 15:20

1 Answers1

5

To expand my comment into a real answer:

Make sure the category's implementation (.m) file is included in your target's Compile Sources build phase. Importing the header is enough to tell the compiler that there is a category on NSProgressIndicator which adds a -start method. Unless the category's implementation is actually compiled and linked into the finished binary (or the method implementation is added at runtime, etc), NSProgressIndicator won't actually respond to the start message at runtime. Because of Objective-C's dynamic message send behavior, there's no way the compiler can tell at compile time whether or not NSProgressIndicator is actually going to respond to that message, which is why you don't get a warning or an error.

Andrew Madsen
  • 21,309
  • 5
  • 56
  • 97