{"id":4966,"date":"2014-03-30T17:18:35","date_gmt":"2014-03-30T17:18:35","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/objective-c-subclass-of-delegate-in-subclass-collection-of-common-programming-errors\/"},"modified":"2014-03-30T17:18:35","modified_gmt":"2014-03-30T17:18:35","slug":"objective-c-subclass-of-delegate-in-subclass-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/objective-c-subclass-of-delegate-in-subclass-collection-of-common-programming-errors\/","title":{"rendered":"Objective-C &mdash; Subclass of delegate in subclass-Collection of common programming errors"},"content":{"rendered":"<p>This is a fairly complicated inheritance hierarchy, so bear with me (I&#8217;ve tried to simplify things rather than state the exact case I am using which is even more complex):-<\/p>\n<p>Let&#8217;s say I create a subclass of <code>UITextField<\/code> called <code>TextField<\/code> which is my own custom enhanced general-purpose textfield. Now, in order to provide this enhanced functionality, in the <code>init<\/code> method of <code>TextField<\/code>, I set <code>super.delegate = self<\/code> so that all the delegate methods from <code>UITextField<\/code> are sent to <code>TextField<\/code>. <code>TextField<\/code> implements the <code>UITextFieldDelegate<\/code> protocol and receives those delegate methods to do something interesting.<\/p>\n<p>However, in turn, I want to make it so that <code>TextField<\/code> has it&#8217;s <em>own<\/em> delegate. So I create a new protocol called <code>TextFieldDelegate<\/code> (note the lack of <code>UI<\/code>-prefix!) and give <code>TextField<\/code> an ivar <code>id __weak delegate<\/code> with corresponding property so that other classes can receive delegate methods from <code>TextField<\/code>.<\/p>\n<p>I hope you&#8217;re still with me, because I haven&#8217;t done anything too complex so far. But let&#8217;s say that now, I create another custom subclass of <code>TextField<\/code>, let&#8217;s call it <code>PasswordTextField<\/code> (in real life, one probably wouldn&#8217;t need to create a subclass just to implement a password functionality, but let&#8217;s assume that there is some fairly sophisticated implementation that would require this).<\/p>\n<p>Let&#8217;s also assume that I want to make it so that <code>PasswordTextField<\/code> (which like <code>TextField<\/code> has a delegate property) is able to send an enhanced set of delegate methods. For example, maybe it can send a method <code>passwordIsSecure<\/code> which is sent once a password has reached a required level of complexity. Now since this behaviour that wouldn&#8217;t be found in the regular <code>TextField<\/code>, I create a new protocol: <code>PasswordTextFieldDelegate<\/code> which defines the new delegate methods for <code>PasswordTextField<\/code> <em>and<\/em> inherits all of the delegate methods sent by <code>TextField<\/code>.<\/p>\n<p>The problem is: how do I do implement this in <code>PasswordTextField<\/code>? Things that don&#8217;t work:<\/p>\n<p><strong>Inheritance<\/strong><\/p>\n<p>I cannot simply inherit the delegate from <code>TextField<\/code>, because <code>TextField<\/code>&#8216;s delegate conforms only to <code>TextFieldDelegate<\/code> and not <code>PasswordTextFieldDelegate<\/code>, so I can&#8217;t send methods like <code>[delegate passwordIsSecure]<\/code> because <code>TextFieldDelegate<\/code> has no such method.<\/p>\n<p><strong>Overriding ivar<\/strong><\/p>\n<p>I could try declaring an ivar in <code>PasswordTextField<\/code> called delegate, but the compiler complains that this is a duplicate declaration, because of course there is already an ivar called delegate in the superclass, so this doesn&#8217;t work either*.<\/p>\n<p><strong>Modifying the superclass<\/strong><\/p>\n<p>I could go back to the <code>TextField<\/code> class and redefine the delegate to implement both <code>TextFieldDelegate<\/code> <strong>and<\/strong> <code>PasswordTextFieldDelegate<\/code>, but this seems messy and tells <code>TextField<\/code> that it can send <code>PasswordTextFieldDelegate<\/code> methods, which of course, it can&#8217;t!<\/p>\n<p>I haven&#8217;t tried this one, simply because it seems to break every sensible coding rule in the book.<\/p>\n<p>In summary, there must be some way of doing this such that a subclass of a class can have it&#8217;s own delegate that&#8217;s a sub-delegate of the superclass&#8217;s delegate and for all of this to fit together nicely, but I just can&#8217;t figure it out! Any ideas?<\/p>\n<p>(* As a side issue, I don&#8217;t understand why the compiler complains when <code>PasswordTextField<\/code> declares a &#8220;duplicate&#8221; ivar named delegate, but doesn&#8217;t complain when <code>TextField<\/code> declares an ivar named delegate which is presumably a duplicate of <code>UITextField<\/code>&#8216;s property called delegate!)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a fairly complicated inheritance hierarchy, so bear with me (I&#8217;ve tried to simplify things rather than state the exact case I am using which is even more complex):- Let&#8217;s say I create a subclass of UITextField called TextField which is my own custom enhanced general-purpose textfield. Now, in order to provide this enhanced [&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-4966","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4966","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=4966"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4966\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}