{"id":4196,"date":"2014-03-30T09:11:20","date_gmt":"2014-03-30T09:11:20","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/whats-the-most-robust-and-readable-way-of-ensuring-objects-conform-to-a-interface-protocol-in-objective-c-collection-of-common-programming-errors\/"},"modified":"2014-03-30T09:11:20","modified_gmt":"2014-03-30T09:11:20","slug":"whats-the-most-robust-and-readable-way-of-ensuring-objects-conform-to-a-interface-protocol-in-objective-c-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/whats-the-most-robust-and-readable-way-of-ensuring-objects-conform-to-a-interface-protocol-in-objective-c-collection-of-common-programming-errors\/","title":{"rendered":"What&#39;s the most robust and readable way of ensuring objects conform to a interface\/protocol in Objective C?-Collection of common programming errors"},"content":{"rendered":"<p>As long as you don&#8217;t use plain <code>id<\/code> as the type, the compiler will at least warn you if you make a mistake at compile time. So you should be fine with your code example #1.<\/p>\n<p>Of course, sometimes you might be forced to work with an <code>id<\/code> object that you get from a subsystem that is not under your control. In such cases you can cast the object back to the type you <em>think<\/em> it has (e.g. <code>id<\/code> ), but you are usually better off if you perform a runtime check first. Better be safe than sorry&#8230;<\/p>\n<p>On a final note: If your protocol has optional parts (declared with the <code>@optional<\/code> keyword), then for those parts you will obviously be able to do runtime checks only. The <code>@required<\/code> keyword mentioned by apurv is necessary only if you want to be explicit in your protocol declaration (a protocol&#8217;s parts are required by default), or if you mix optional and required parts.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As long as you don&#8217;t use plain id as the type, the compiler will at least warn you if you make a mistake at compile time. So you should be fine with your code example #1. Of course, sometimes you might be forced to work with an id object that you get from a subsystem [&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-4196","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4196","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=4196"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4196\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}