{"id":4257,"date":"2014-03-30T09:31:40","date_gmt":"2014-03-30T09:31:40","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/methods-in-super-that-will-be-subclassed-anyway-cocoa-collection-of-common-programming-errors\/"},"modified":"2014-03-30T09:31:40","modified_gmt":"2014-03-30T09:31:40","slug":"methods-in-super-that-will-be-subclassed-anyway-cocoa-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/methods-in-super-that-will-be-subclassed-anyway-cocoa-collection-of-common-programming-errors\/","title":{"rendered":"Methods in super that will be subclassed anyway (Cocoa)-Collection of common programming errors"},"content":{"rendered":"<p>My solution was a little weird, but here it is:<\/p>\n<pre><code>@protocol JSDog \n- (void)yipe;\n@end\n\n@interface JSDog : NSObject\n@end\n\n@implementation JSDog\n\n+ (void)initialize {\n  if ([self isSubclassOfClass:[JSDog class]] &amp;&amp; ![self conformsToProtocol:@protocol(JSDog)]) {\n    NSAssert(false, @\"Subclasses of JSDog must conform to .\");\n  }\n}\n\n@end\n<\/code><\/pre>\n<p>Having a protocol with the same name as a class is precedented in <code>NSObject<\/code>. Because methods in a formal protocol a by default <code>@required<\/code>, you will be protected on both ends: in <strong>compile-time<\/strong>, if your <code>JSDog<\/code> subclass purports to conform to , but doesn&#8217;t implement <code>-yipe<\/code>, you will receive an error; at <strong>runtime<\/strong>, if your subclass does not claim to conform with , you will receive a warning when the subclass is instantiated.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My solution was a little weird, but here it is: @protocol JSDog &#8211; (void)yipe; @end @interface JSDog : NSObject @end @implementation JSDog + (void)initialize { if ([self isSubclassOfClass:[JSDog class]] &amp;&amp; ![self conformsToProtocol:@protocol(JSDog)]) { NSAssert(false, @&#8221;Subclasses of JSDog must conform to .&#8221;); } } @end Having a protocol with the same name as a class is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4257","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4257","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4257"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4257\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}