[cfe-dev] Objective-C: Protocol method implemented in class is not recognised by category conforming to protocol

Aidan Steele cfe-dev at aidans.org
Tue Apr 3 18:00:08 PDT 2012


Hi all,

Sorry for the mouthful of a subject line, but I wanted it to be
descriptive. :) I have the following code:

	#import <Foundation/Foundation.h>
	
	@interface Test : NSObject
	- (id)testMethod;
	@end
	
	@implementation Test
	
	- (id)testMethod;
	{
	  return nil;
	}
	
	@end
	
	@protocol TestProtocol <NSObject>
	- (id)testMethod;
	- (id)secondMethod;
	@end
	
	@interface Test (TestCategory) <TestProtocol>
	@end
	
	@implementation Test (TestCategory)
	
	- (id)secondMethod;
	{
	  return nil;
	}
	
	@end

It generates the following warnings:


	$ clang test.m
	test.m:24:17: warning: incomplete implementation [-Wincomplete-implementation]
	@implementation Test (TestCategory)
					^
	test.m:24:17: warning: method in protocol not implemented [-Wprotocol]
	test.m:17:1: note: method declared here
	- (id)testMethod;
	^
	test.m:21:12: note: required for direct or indirect protocol 'TestProtocol'
	@interface Test (TestCategory) <TestProtocol>
			   ^
	2 warnings generated
	

As you can see, the method declared in the protocol is defined in the
original class implementation and not the category that declares its
conformance to the protocol. I can't re-define the method in the
category and call through to "super" as that would go through to
NSObject.

My question is this: is this (a) a bug in clang, (b) a bug in my
understanding or (c) a combination of the above?



More information about the cfe-dev mailing list