1

I am a beginner to programming, and a beginner to Objective-C. I learned basic C and decided to start learning Objective-C. I am reading "Programming in Objective C 2.0" by Steven Kochan. His section on Protocols is vague. He doesn't thoroughly explain WHY someone would want to use protocols in their programs, nor does he give a concrete example with it implemented in a program. He writes: "You can use a protocol to define methods that you want other people who subclass your class to implement." He also says that Protocols are good for sub-classes to be able to implement certain methods, without having to first define the actual methods. He also says protocols can be used across different classes because they are classless.

I know there must be a valid and smart way to implement protocols, but based on what he wrote, I don't see why someone would use protocols instead of just creating a class method outside of the reason that more than one class can adhere to a protocol (I know there are some more good reasons though!). I was wondering if someone could help me understand: how, why and when I would use Protocols in my program in an intelligent way.

SaamJB
  • 221
  • 5
  • 16

5 Answers5

5

If you've done any kind of object-oriented programming, you probably know protocols as interfaces (they're not identical, but the concept is similar). If not, think of protocols as blueprints.

The main reason why you'd use protocols is so you can use objects without knowing everything about them; all you need to know is that they implement a set of methods. For example, if the classes Business and Person conform to the protocol Contact, which defines the method - (NSString *)phoneNumber, the class AddressBook can call -(NSString *)phoneNumber without knowing whether or not the object is of type Business or Person.

Once you start to learn about Cocoa and delegates, you'll see how powerful and important protocols are.

conmulligan
  • 7,038
  • 6
  • 33
  • 44
4

One word, delegates.
Objective-c uses delegates all over the place to allow classes to talk to each other.
To see an example see UITableViewDelegate Protocol
That's not the only place @protocol is used, but it's probably the most common use for it.

bluevial
  • 96
  • 1
  • 1
  • 4
2

Protocols are better versions of callback functions in C. Protocols are useful constructs when you want to implement MVC architecture yourself. The Views need to be notified when Model changes,You can use protocols to notify appropriate events to Observers.

Shreesh
  • 677
  • 5
  • 15
0

You could have a class that is a UIViewController, and it implements several protocols, such as UITableViewDelegate, UITableViewDataSource. A class can do more than one thing.

Rayfleck
  • 12,116
  • 8
  • 48
  • 74
0

Like @conmulligan says Objective-C uses protocols to make classes talk to each other.

Its one of many ways to communicate between classes.

But protocols is necessarily a bad way.

I use protocols if I was to create a re-usable object, that is usually used for many projects.

So I create protocols to make my code easy to maintain.

t0mm13b
  • 34,087
  • 8
  • 78
  • 110
wei
  • 4,267
  • 2
  • 23
  • 18