Delegation and Protocols



I’m trying to use a custom Delegation and I have a doubt about the use of the protocols.

What I did is,

1.- define a class that will be my delegate.
2.- in my controller class I defined an instance of my delegate class on the @interface.

myDelegateClass *mDc;

3.-in the init method, I delegate myDelegateClass to an object (after the alloc and init);

[anObject setDelegate: mDc];

4.- finally, I implemented the methods I need in the @implementation of myDelegateClass.

If all that is correct,

Is it necessary to use / define a protocol and tell to myDelegateClass that it conforms it adding it to its definition?

myDelegateClass: NSObject { }

Or instead of that I can define the methods I need as a normal class methods without tell any protocol conforming to myDelegateClass?

Thanks for any help.



It isn’t “necessary” to define a custom protocol and as long as all the required methods are implemented then it will work fine.

However it puts the onus on you to make sure that they are all present and correct - whereas if you use a protocol then the compiler will tell you.

It’s not a big deal for a small program with a few required methods but I would get in the habit of doing so as it will save you from a lot of headaches once your projects start growing.