Making class conform to protocol with category for existing methods

淺唱寂寞╮ 提交于 2019-11-30 11:51:30

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;
}

You might consider this a bit of a hack, and I don't know if it will even work, but how about declaring it as a read only property

@property (readonly, assign) NSUInteger length;

and then in the implementation of your category, make it @dynamic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!