views:

52

answers:

3

Hi, I'm very curious - why does UITextInputDelegate have all its methods required? Why can't I just implement the one I want (for example textDidChange)?

A: 

For me it looks like some apple engineer was too lazy to program the delegate calls properly. So he just didn't set the @optional so he doesn't have to deal with methods that probably not exist.

V1ru8
deleted........
JeremyP
i don't think that's it.
Merlin
If that were the case I am sure Apple would have fixed it, after all changing delegates to optional is not going to break existing code.
Simon
Well what happens when your existing code calls an optional method that is not implemented? It will crash! You have to check optional methods if they're there. You don't have to do that on required methods! (see @JeremyP's argument)
V1ru8
yeah, but I don't think laziness has anything to do with it.
Merlin
yeah, yeah, maybe they have some reason for doing it. Who knows.
V1ru8
A: 

It might be a performance thing. These methods are called frequently and it's quite a big performance hit to have to send -respondsToSelector: every time anything changes.

JeremyP
Could be, but you don't have to call respondsToSelector on every call. You could check that only when the delegate changes. This even might be faster because then you only compare to a boolean which I assume is faster then calling an empty method.
V1ru8
so mystery remains :) I'm approving this answer because it sounds reasonable.
Merlin
Thanks Merlin. Just to be clear, the answer is just speculation. I don't have any secret information from Apple.
JeremyP
A: 

Can't you just make empty funcs for the three methods you don't need?

ExitToShell
I can and I do, I'm just curious why I have to :)
Merlin