Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

objective c init in protocol

yesterday a colleague asked, why we should not declare an init method (initWith...:(...)) in a protocol to force implementing classes to supply such an initializer. I was quite suprised about that question and in my understanding, this is nonsense. But I could not find a definitive reason but that declaring an init method in a protocol leads to less flexibility for the implementations.

Could you please tell me a good reason, why there should or should not be an init method in a protocol?

Thanks!

like image 790
SeriousBob Avatar asked May 29 '13 09:05

SeriousBob


People also ask

What is init in Objective-C?

init() Implemented by subclasses to initialize a new object (the receiver) immediately after memory for it has been allocated.

What is Objective-C protocol?

Objective-C allows you to define protocols, which declare the methods expected to be used for a particular situation. This chapter describes the syntax to define a formal protocol, and explains how to mark a class interface as conforming to a protocol, which means that the class must implement the required methods.

How do you call an init method in Objective-C?

Whenever an object is "created" by default it is supposed to be done with myObject=[[MyObjectClass alloc]init] or the equivalent shortcut myObject=[MyObjectClass new] . That is where your init method is called.

Can Objective-C protocols have properties?

You can have properties in a protocol, provided every class that conforms to your protocol have a corresponding @synthesize for that property, or provide a getter and setter.


1 Answers

You define methods in protocols so that your code could call methods implemented by others. The "contract" between you and developers implementing your protocol looks like this:

  • You define the protocol,
  • Someone else implements your protocol,
  • Someone else creates an object implementing your protocol, and gives it to you, so
  • You can call methods of your protocol without knowing their implementation.

In order to call methods of your protocol, you need to have an instance of an object implementing it. The whole point of defining protocols is removing from your code any knowledge about the class implementing your protocol: if you know which class you are going to get, you might as well skip the protocol, and program to the class directly. However, if you want to call your init, you have to either know the class, or someone else must pass you an alloc-ed object on which the init has not been called yet. Neither alternative is a good idea - the first one kills the purpose of having protocols, and the second forces your callers deal in partially initialized objects.

Note that it does not prevent you from having non-init configuration methods in a protocol: if all objects must be configured using certain bits of information, let your users implement whatever init that they want, and add a configureWith:... method to your protocol, letting you control the process of configuring the object without knowing about its init method.

like image 79
Sergey Kalinichenko Avatar answered Sep 29 '22 11:09

Sergey Kalinichenko