I am using a Object method inside the @protocol , but protocol only supports method declaration , could someone help me about how to connect that with the method definition of the same method .(where to write the method Definition).
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Protocols are just a list of method declarations without any implementation. They can be used to restrict a set of possible (classes) to those that implement a certain protocol.
For instance if you had a
GasStationclass you might want to restrict the type (class) of objects that can be refueled at the station. Of course you could simply require the objects to be a descendant of the classVehicle, like this:But this wouldn’t allow a jerrycan to be filled up for instance. However allowing any type of object to be passed is a bad idea either, because if you passed an instance of
Catyou would certainly get an error when trying to send it anaddFuel:message.So instead you define a protocol that refuelable classes have to implement:
Now you can use that protocol to restrict what types of objects can be refueled:
A
Vehiclewill of course have a methodaddFuel:and so canJerryCanimplement its own separate version, even though it’s not aVehicle.