Skip to main content

ad

Learn ios delegates and protocols.

A protocol, declared with the (@protocol syntax in Objective-C) is used to declare a set of methods that a class "adopts" (declares that it will use this protocol) will implement. This means that you can specify in your code that, "you don't care which class is used as long as it implements a particular protocol". This can be done in Objective-C as follows:
id<MyProtocol> instanceOfClassThatImplementsMyProtocol;
@protocol MyProtocol <NSObject>

- (void)aRequiredMethod;

@required
- (void)anotherRequiredMethod;

@optional
- (void)anOptionalMethod;

@end
@interface MyClass <MyProtocol>

@end
You usually keep reference to an object conforming to a protocol using a property. For example, to keep track of a delegate:
@property (nonatomic, weak) id<MyProtocol> delegate;
At this point, in your code, you just have to call the method you want to call on the object that you're keeping reference of and that implements your protocol as you would with any other method:
[self.delegate aRequiredMethod];
To check wether an object conforms to a protocol you can call
[self.delegate conformsToProtocol:@protocol(MyProtocol)]
To check wether an object implements a method you can call
[self.delegate respondsToSelector:@selector(anOptionalMethod)]
Note -
If you state this in your code, then any class that "conforms" to the protocol MyProtocol can be used in the variable instanceOfClassThatImplementsMyProtocol. This means that the code that uses this variable knows that it can use whichever methods are defined in MyProtocol with this particular variable, regardless of what class it is. This is a great way of avoiding the inheritance design pattern, and avoids tight coupling.
Delegates are a use of the language feature of protocols. The delegation design pattern is a way of designing your code to use protocols where necessary. In the Cocoa frameworks, the delegate design pattern is used to specify an instance of a class which conforms to a particular protocol. This particular protocol specifies methods that the delegate class should implement to perform specific actions at given events. The class that uses the delegate knows that its delegate coforms to the protocol, so it knows that it can call the implemented methods at given times. This design pattern is a great way of decoupling the classes, because it makes it really easy to exchange one delegate instance for another - all the programmer has to do is ensure that the replacement instance or class conforms to the necessary protocol (i.e. it implements the methods specified in the protocol)!
Protocols and delegates are not restricted only to Objective-C and Mac/iOS development, but the Objective-C language and the Apple frameworks make heavy use of this awesome language feature and design pattern.


Comments

ads

Popular posts from this blog

Do you know about the Apple Wallet ? Now You can pay with apple wallet, How ? Learn with us.

Apple Wallet  (referred to as simply  Wallet ) is an application in  Apple 's iOS (previously known as Passbook  in iOS 6 to iOS 8) that allows users to store coupons, boarding passes, event tickets, store cards and, starting with iOS 8.1, credit cards, loyalty cards, and debit cards via  Apple  Pay. With Wallet, you can keep your credit, debit, and prepaid cards, store cards, boarding passes, movie tickets, coupons, rewards cards, and more in one place. Add passes: You can add passes to Wallet in several ways: Using Wallet-enabled apps With Mail or Messages Through a web browser Scanning a barcode Sharing through AirDrop From your Mac Tapping a Wallet notification that you got after paying with Apple Pay at a supported merchant   Use your passes Some passes automatically appear at the right time or place because they include information based on time or location. For example, when you arrive at the airport, you...

Apple iOS New Programming Language Swift Introduction.

The powerful programming language that is also easy to learn. Swift is a powerful and intuitive programming language for macOS, iOS, watchOS and tvOS. Writing Swift code is interactive and fun, the syntax is concise yet expressive, and Swift includes modern features developers love. Swift code is safe by design, yet also produces software that runs lightning-fast. Introducing  Swift 3 Swift 3  is a thorough refinement of the language and the API conventions for the frameworks you use every day. These improvements make the code you write even more natural, while ensuring your code is much more consistent moving forward. For example, select Foundation types such as the new  Date  type are easier to use and are much faster than previous releases, and the  Calendar  type uses enums to feel more at home within Swift. Open Source Swift 3 is the first major release developed in the open at Swift.org, with source code, a bug tracker, mailing li...

Working with Blocks in iOS | Objective-C | Importance of Blocks in iOS Programming.

An Objective-C class defines an object that combines data with related behavior. Sometimes, it makes sense just to represent a single task or unit of behavior, rather than a collection of methods. Blocks are a language-level feature added to C, Objective-C and C++, which allow you to create distinct segments of code that can be passed around to methods or functions as if they were values. Blocks are Objective-C objects, which means they can be added to collections like  NSArray  or  NSDictionary . They also have the ability to capture values from the enclosing scope, making them similar to  closures  or  lambdas  in other programming languages. This chapter explains the syntax to declare and refer to blocks, and shows how to use blocks to simplify common tasks such as collection enumeration. For further information, see  Blocks Programming Topics . Here is the apple reference:  https://developer.apple.com/library/content/docu...