Skip to content

Instantly share code, notes, and snippets.

@karolkozub
Last active August 29, 2015 14:22
Show Gist options
  • Save karolkozub/7af9f3a05cdbd707c2c0 to your computer and use it in GitHub Desktop.
Save karolkozub/7af9f3a05cdbd707c2c0 to your computer and use it in GitHub Desktop.
Classes don't conform to protocols adopted through categories without matching implementations
#import <Foundation/Foundation.h>
@protocol ProtocolOne <NSObject>
@optional
- (void)one;
@end
@protocol ProtocolTwo <NSObject>
@optional
- (void)two;
@end
@protocol ProtocolThree <NSObject>
@optional
- (void)three;
@end
@protocol ProtocolFour <NSObject>
@optional
- (void)four;
@end
@interface Test : NSObject <ProtocolOne>
@end
@interface Test () <ProtocolTwo>
@end
@interface Test (CategoryWithMatchingImplementation) <ProtocolThree>
@end
@interface Test (CategoryWithOnlyTheInterface) <ProtocolFour>
@end
@implementation Test
@end
@implementation Test (CategoryWithMatchingImplementation)
@end
int main(int argc, char *argv[]) {
@autoreleasepool {
NSLog(@"[Test conformsToProtocol:ProtocolOne] ? %@", [Test conformsToProtocol:@protocol(ProtocolOne)] ? @"YES" : @"NO");
NSLog(@"[Test conformsToProtocol:ProtocolTwo] ? %@", [Test conformsToProtocol:@protocol(ProtocolTwo)] ? @"YES" : @"NO");
NSLog(@"[Test conformsToProtocol:ProtocolThree] ? %@", [Test conformsToProtocol:@protocol(ProtocolThree)] ? @"YES" : @"NO");
NSLog(@"[Test conformsToProtocol:ProtocolFour] ? %@", [Test conformsToProtocol:@protocol(ProtocolFour)] ? @"YES" : @"NO");
// Output:
// 2015-05-31 22:15:42.612 Untitled 7[1996:53196] [Test conformsToProtocol:ProtocolOne] ? YES
// 2015-05-31 22:15:42.612 Untitled 7[1996:53196] [Test conformsToProtocol:ProtocolTwo] ? YES
// 2015-05-31 22:15:42.612 Untitled 7[1996:53196] [Test conformsToProtocol:ProtocolThree] ? YES
// 2015-05-31 22:15:42.612 Untitled 7[1996:53196] [Test conformsToProtocol:ProtocolFour] ? NO
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment