Why use protocol, not call the method directly

delegatesiosobjective cprotocol

I was asked this question in an interview. For eg: UITableviewDelegate protocol has CellForRowAtIndexpath. Why make it a delegate method in a protocol not a method in the UITableView class and call it directly?

Best Answer

Protocols are used to define a set of methods to interact with regardless of the type of the object implementing them. It allows you to easily interchange external resources that the view interacts with and reduces the overall coupling of your code. This is in contrast to directly calling the resource with knowledge of its type.

Related Topic