Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking class conform to protocol with category for existing methods
    primarykey
    data
    text
    <p>I have a protocol named MyProtocol. MyProtocol has an required method:</p> <pre><code>- (NSUInteger)length; </code></pre> <p>And some other methods.</p> <p>Now i want to make the NSString class conform to MyProtocol with a category. Like so:</p> <pre><code>@interface NSString (NSStringWithMyProtocol) &lt;MyProtocol&gt; @end </code></pre> <p>In this category i implement all methods excluding the 'length' method, because i want the original NSString implementation. I do not want to override it in this particular class.</p> <p>Now i get a warning because of an incomplete implementation of MyProtocol in the category.</p> <p>I know there are a few solutions to solve this.</p> <ol> <li>Make the method optional</li> <li>Pointer Swizzling</li> <li>Adding subclass to class which is conform to the protocol. Then leave out the implementation.</li> </ol> <p>I do not want to use these options because they result in a bad design for the rest of my code.<br> Option 3 is bad, because of existing direct subclasses will not be conform to the protocol.</p> <p>Does anybody know how to remove the warning without implementing the length method?</p> <p><strong>NOTE:</strong> The class, category and protocol are just examples. I did encounter this problem with other classes which i could not post about. Thanks</p> <p><strong>EDIT:</strong> Added the third option.</p> <p><strong>Full Code:</strong></p> <p>The protocol:</p> <pre><code>@protocol MyProtocol &lt;NSObject&gt; - (void) myMethod; - (NSInteger) length; @end </code></pre> <p>The category header:</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import "MyProtocol.h" @interface NSString (MyProtocol) &lt;MyProtocol&gt; @end </code></pre> <p>The category implementation:</p> <pre><code>@implementation NSString (MyProtocol) - (void)myMethod { } @end </code></pre> <p>This results in the following warnings.</p> <pre><code>Incomplete implementation Method in protocol not implemented </code></pre> <p>In this screenshot you can see my warning: <img src="https://i.stack.imgur.com/qG1V9.png" alt="Screen shot"></p> <p>I tried compiling with LLVM GCC 4.2 and the Apple LLVM 3.0 compiler. I also compiled on xcode 4.0.2 and Xcode 4.2. I'm on OS X 10.6.8.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload