-1

I have a simple protocol called as OnSuggestionTextChanged

@protocol OnSuggestionTextChanged <NSObject>
-(void)onTextChanged:(NSString*)newText;
@end

And I have another protocol called as TextEditable which has this protocol as a property

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

@protocol TextEditable <NSObject>

@required
-(void)setTextString:(NSString*)text;
-(NSString*)textString;
-(void)notifySuggestionButtonPressed;
-(NSInteger)cursorPosition;

@property(nonatomic,weak)id<OnSuggestionTextChanged> onSuggestionTextChange;

@end

But in my custom UITextView which conforms to the protocol TextEditable.

When I try to access the property OnSuggestionTextChanged I get a:

unrecognized selector for [CustomTextView onSuggestionTextChanged] (not for onTextChanged)

which is really weird because Xcode doesn't throw a compiler error but a runtime error.

Can you please tell me if what I am trying to do is really possible. If so, why am I getting unrecognized selector?

Just in case you guys don't believe me.

enter image description here

Unheilig
  • 16,196
  • 193
  • 68
  • 98
Tapan Thaker
  • 362
  • 5
  • 13
  • 3
    If a protocol includes a `@property`, that does *NOT* mean the property is now part of all implementing classes. Rather, it's *your* responsibility in classes adhering to the protocol (like your `UITextView` subclass) to ensure that the property exists. See: http://stackoverflow.com/a/844785/88111 Also, as @rmaddy mentions below, adding `@synthesize` to your subclass should generate the getters/setters for you. – Craig Otis Sep 04 '14 at 14:27
  • 4
    In your class that conforms to the `TextEditable` protocol, try adding `@synthesize onSuggestionTextChange;`. – rmaddy Sep 04 '14 at 14:27
  • 2
    Using properties in protocol is bad practise. You should rethink your protocol. – Cy-4AH Sep 04 '14 at 14:35
  • 2
    Btw. I have checked. Xcode throws compiler warning: `Auto property synthesis will not synthesize property declared in a protocol`. – Cy-4AH Sep 04 '14 at 14:41
  • 1
    you need to implement the _setter_ and _getter_ for that property e.g. using the `@synthesize`, if the default ones are good enough for you. if not, you can still implement own ones. – holex Sep 04 '14 at 14:56
  • @Cy-4AH : Can you justify why using a property in the protocol is a bad practice ? – Tapan Thaker Sep 04 '14 at 15:21
  • @TapanThaker, protocols should do some stuff with data, not provide data. – Cy-4AH Sep 04 '14 at 15:30
  • My problem is this: I want a Editable so that I can abstract myself from UITextView or UITextField, and I want my Editable to receive callbacks when text has been changed. So it seemed reasonable to have a delegate property to send callbacks – Tapan Thaker Sep 04 '14 at 15:49

1 Answers1

0

Answering my own question here :

properties defined in protocols are not synthesized automatically, instead you need to synthesize them in the class that conforms to the protocol.

Also, I didn't receive a compile time error, but there is a warning as @Cy-4AH stated that says :

Auto property synthesis will not synthesize property declared in a protocol

Thank you guys for the comments above.

Tapan Thaker
  • 362
  • 5
  • 13