<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><blockquote type="cite" class="">On Nov 15, 2016, at 7:27 PM, Karl via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""></blockquote><div><blockquote type="cite" class=""><br class="Apple-interchange-newline"><div class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">In Objective-C, asking whether or not an object conforms to a protocol just cascades in to a bunch of calls to “respondsToSelector”, so it’s also very painful.</span></div></blockquote></div><br class=""><div class="">This isn’t true; Objective-C stores a list of protocols that a class conforms to, so -respondsToSelector: does not need to be called when checking protocol conformance (also: simply adopting a protocol’s methods will not cause a class to conform to the protocol).</div><div class=""><br class=""></div><div class="">You can test this yourself:</div><div class=""><br class=""></div><div class=""><div class="">#import <Foundation/Foundation.h></div><div class=""><br class=""></div><div class="">@protocol P</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span></div><div class="">- (void)foo;</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span></div><div class="">@end</div><div class=""><br class=""></div><div class="">@interface C: NSObject<P></div><div class=""><br class=""></div><div class="">- (void)foo;</div><div class=""><br class=""></div><div class="">@end</div><div class=""><br class=""></div><div class="">@implementation C</div><div class=""><br class=""></div><div class="">- (void)foo {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>NSLog(@"foo");</div><div class="">}</div><div class=""><br class=""></div><div class="">- (BOOL)respondsToSelector:(SEL)selector {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>NSLog(@"respondsToSelector: called");</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>return [super respondsToSelector:selector];</div><div class="">}</div><div class=""><br class=""></div><div class="">@end</div><div class=""><br class=""></div><div class="">int main(int argc, char *argv[]) {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>@autoreleasepool {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>C *c = [C new];</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span></div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>NSLog(@"C is P: %@", [c conformsToProtocol:@protocol(P)] ? @"YES" : @"NO");</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div class="">}</div></div><div class=""><br class=""></div><div class="">The output is only:</div><div class=""><br class=""></div><div class=""><b class="">C is P: YES</b></div><div class=""><b class=""><br class=""></b></div><div class="">The log we put in -respondsToSelector: never gets printed.</div><div class=""><br class=""></div><div class="">Charles</div><div class=""><br class=""></div></body></html>