[Protocol in Hmly]12.ios

Source: Internet
Author: User

Recently I encountered a confusing thing in my work, when I used runtime to get the properties of a class, because the class implements a custom protocol, the traversed attribute contains the property in the NSObject protocol. Check to find out, just know and protocol about. In the evening asked the next friend (iOS big God), the result was his sentence dissected. Found this part of the knowledge point missing a little.

Protocol is similar to interface in Java, and is primarily used to define communication rules between a set of objects. Protocol is also a common thing in our design, compared to the way of direct inheritance, protocol is biased towards the combination mode. Because in the design of objects, if the base of a lot of things, and different subclasses do not necessarily need the base class of things, or in the vast majority of the needs of the same time, there are special requirements, then this time is very chaotic. Using interfaces, different functions can be categorized into different interfaces, so that the subclass needs what function, to implement the interface itself, so that while maintaining the inheritance, the function can be extended, without affecting other classes, but also let the subclass maintain its own uniqueness.

Protocol syntax:
@protocol protocolName//method//property@end

Similar to the declaration of a class, you can declare not only methods, but also declarations property . If a class needs to implement an interface, you only need to add <> after the declaration of the class, in which the protocol name to implement is written, and multiple protocols are separated by commas.
@interface Test : NSObject <delegate1, delegate2>@end


Protocol keyword: required,optional
@protocol TestProtocol@required- (void)requiredMethod;@optional- (void)optionalMethod;@end

Protocol is required by default, and a class must implement these methods when implementing the Protocol. In contrast, if optional the following method, it means that the class can be optionally implemented. To determine whether this class implements a method, simply call the[self.delegate respondToSelector:@selector()]
Inheritance of protocol

Protocol, like classes, can inherit as well, and this is one of my omissions.

@protocol Test1Delegate@end@protocol Test2Delegate <Test1Delegate>@end;

At this time, if the class implements the Test2delegate protocol, it must also implement the Test1delegate inside the method. When we write our own protocol, the general Xcode is the default to help us inherit the NSObject protocol. If you do not inherit the words have no big impact, because our objects are inherited from NSObject, and NSObject also implemented NSObject this protocol. So, when we need to invoke the method in the NSObject protocol, there is no error. However, Apple still recommends inheriting the NSObject protocol.


protocol types of hidden classes

This also happens in our iOS development, such as IOS7 's navigation bar animations, and Apple just needs you to return an object that implements the UIViewControllerAnimatedTransitioning protocol.

There is also a possibility to deal with the third-party SDK when it is more. In other people's implementation of the framework, sometimes, do not want to put the type of the class and the method of exposure to you, and you are not likely to directly create this object. You can use protocol this way so that the caller does not need to know the type of the class and can do what they want.

id <Test1Delegate>obj = [XXXX  createObj];

This is the only way for callers to [XXXX createObj] get an instance of the implementation test1delegate without knowing the type. Where needed, this obj can directly invoke the method inside the protocol, because this object has already been implemented.

Protocol is almost so much content, relatively simple.

[Protocol in Hmly]12.ios

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.