Is it a good practice to force the developer to comply with a protocol in Goal C?


I am developing a simple set of reusable code. I have created a protocol to group a set of methods and ask the user at run time for data.

I think that setting a delegate would be mandatory. Can I force the developer at runtime to set the delegate by throwing an assert stating the reason?

Is this a good practice to enforce the user of my classes by using asserts? Does it have a design level flaw?

First the "good practice" question:

  • Don't make methods required unless they actually are.
  • Don't make methods optional if they aren't.

  • Not a critical one, more of an opinion

    • Provide a method initWithDelegate: if a delegate is required
    • I often come across data structures where I don't know that there is a delegate pattern until I investigate. With the specific init method, I'm made aware of it earlier.

You can cause warnings at compile time by simply adding the @required indicator to your protocol:

@protocol SuperAwesomeProtocol <NSObject>

-(void) requiredMethod1;
-(void) requiredMethod2;

-(void) optionalMethod1;
-(void) optionalMethod2;


//and making your delegate specific:
@property (nonatomic, assign) id<SuperAwesomeProtocol> delegate;

You can still cause runtime asserts with:

if (!delegate || ![delegate respondsToSelector:@selector(requiredMethod1)])
    //Assertion failure