views:

30

answers:

1

Is this possible? For example, I have a class TriangleDataView with an init method. Currently the method is declared like this:

- (id)initWithFrame:(CGRect)frame delegate: (id) delegateObject;

I would like to require that delegateObject conform to the UITextFieldDelegate protocol. But what is the syntax for that?

+4  A: 
- (id)initWithFrame:(CGRect)frame delegate:(id<UITextFieldDelegate>)delegateObject;
Marcelo Cantos
+1: But, I believe this just raises a warning. If he really wants to check he could add `if (![delegateObject conformsToProtocol: @protocol(UITextFieldDelegate)])` and throw an exception. Maybe you could add that to have a more complete answer?
sdolan
@sdolan, the better answer for that is -Werror (Treat Warnings as Errors). ObjC relies heavily on warnings for proper coding, and you should never ignore them.
Rob Napier
@Rob Napier: Thanks, I didn't know about that flag (though I do the same thing in practice). The only advantage to my suggestion is if you're distributing the code to other devs that don't have that flag, don't fix warning, and can't debug "doesNotRespondToSelector" exceptions caused by not implementing the protocol in seconds.
sdolan
+1 @Rob: That's the first flag anyone should turn on for a new project.
Marcelo Cantos