Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making class conform to protocol with category for existing methods

I have a protocol named MyProtocol. MyProtocol has an required method:

- (NSUInteger)length;

And some other methods.

Now i want to make the NSString class conform to MyProtocol with a category. Like so:

@interface NSString (NSStringWithMyProtocol) <MyProtocol>
@end

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.

Now i get a warning because of an incomplete implementation of MyProtocol in the category.

I know there are a few solutions to solve this.

  1. Make the method optional
  2. Pointer Swizzling
  3. Adding subclass to class which is conform to the protocol. Then leave out the implementation.

I do not want to use these options because they result in a bad design for the rest of my code.
Option 3 is bad, because of existing direct subclasses will not be conform to the protocol.

Does anybody know how to remove the warning without implementing the length method?

NOTE: The class, category and protocol are just examples. I did encounter this problem with other classes which i could not post about. Thanks

EDIT: Added the third option.

Full Code:

The protocol:

@protocol MyProtocol <NSObject>

- (void) myMethod;
- (NSInteger) length;

@end

The category header:

#import <Foundation/Foundation.h>
#import "MyProtocol.h"

@interface NSString (MyProtocol) <MyProtocol>
@end

The category implementation:

@implementation NSString (MyProtocol)

- (void)myMethod {

}

@end

This results in the following warnings.

Incomplete implementation

Method in protocol not implemented

In this screenshot you can see my warning: Screen shot

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.

like image 952
Mats Stijlaart Avatar asked Aug 31 '11 10:08

Mats Stijlaart


People also ask

How do you conform to a protocol in Objective-C?

Objective-C uses angle brackets to indicate conformance to a protocol. This example declares a weak property for a generic object pointer that conforms to the XYZPieChartViewDataSource protocol.

How do you ensure the adoption of a protocol only by reference type?

You can limit protocol adoption to class types (and not structures or enumerations) by adding the AnyObject or class protocol to a protocol's inheritance list.

What's the difference between a protocol and a class in Swift?

You can create objects from classes, whereas protocols are just type definitions. Try to think of protocols as being abstract definitions, whereas classes and structs are real things you can create.

Is it possible to prevent the adoption of a protocol by a struct?

The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements. But there would be a time when you want to restrict protocols to be adopted by a specific class. In Swift 5, you can do just that.


1 Answers

I cannot reproduce this issue. Can you post code that demonstrates it? The following compiles without warnings on 10.7.

#import <Foundation/Foundation.h>

@protocol MyProtocol <NSObject>
- (NSUInteger)length;
@end

@interface NSString (NSStringWithMyProtocol) <MyProtocol>
@end

int main (int argc, const char * argv[]) {
  @autoreleasepool {
    id<MyProtocol> foo = @"foo";
    NSLog(@"%@", foo);    
  }
  return 0;
}
like image 183
Rob Napier Avatar answered Oct 22 '22 23:10

Rob Napier