1.What is Delegate & Protocol in iOS?

1.What is Delegate & Protocol in iOS?
A.Protocol:- 
1.A Protocol declares a programmatic interface that any class may choose to implement.Protocol make it possible for two classes that related by inheritance to communicate with each other to accomplish a certain goal.
2.A Protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements. Any type that satisfies the requirements of a protocol is said to conform to that protocol.






There are two varieties of protocol 
Informal Protocol : Category (Implementations are Optional)
Formal Protocol : Extension (Implementations are Optional and required)
1.A formal protocol :- it declares a list of methods that client classes are expected to Implement.Its implementation requires 1.required 2.Optional methods.A formal protocol can also adopt other protocols.
2.An informal protocol is category on NSObject ,which implicitly make almost all objects adopters of the protocol.
id <sampleprotocol>Obj;
@property (nonatomic, weak) id<MyProtocol> delegate;
Protocol Syntax:-
@protocol MyProtocol <NSObject>

- (void)aRequiredMethod;

@required
- (void)anotherRequiredMethod;

@optional
- (void)anOptionalMethod;

@end
.


Swift Syntax:- 

protocol SomeProtocol {
.     // protocol definition goes here
. }

We can add multiple protocol after the : .Like given example struct protocol
. struct SomeStructure: FirstProtocol, AnotherProtocol {
.     // structure definition goes here

. }

Comments

Popular posts from this blog

Google drive 60 new templates

Natural sense of touch restored with bionic hand

Mac OS X 10.9 (Mavericks) at Cornell