![]() Note: The method declaration in a protocol is just like any other declaration. To specify that one protocol conforms to another, you provide the name of the other protocol in angle brackets, like this: The protocol adoption is useful, however, for situations like that described above. Because you’re presumably using some subclass of NSObject, you don’t need to worry about providing your own implementations for these NSObject methods. In the same way that an Objective-C class can inherit from a superclass, you can also specify that one protocol conforms to another.Īs an example, it’s best practice to define your protocols to conform to the NSObject protocol (some of the NSObject behavior is split from its class interface into a separate protocol the NSObject class adopts the NSObject protocol).īy indicating that your own protocol conforms to the NSObject protocol, you’re indicating that any object that adopts the custom protocol will also provide implementations for each of the NSObject protocol methods. One way to avoid the compiler error is to set the custom protocol to adopt the NSObject protocol. Once you qualify an id with a protocol, all static type-checking comes back you’ll get an error if you try to call any method that isn’t defined in the specified protocol. If you attempt to call the respondsToSelector: method on an id conforming to the protocol as it’s defined above, you’ll get a compiler error that there’s no known instance method for it. Remember: Local object variables are automatically initialized to nil. You can mark protocol methods as optional using the directive, like this: If the data source object doesn’t implement the titleForSegmentAtIndex:, no titles should be shown in the view. ![]() These are methods that a class can implement only if it needs to.Īs an example, you might decide that the titles on the pie chart should be optional. It’s also possible to specify optional methods in a protocol. This means that any class that conforms to the protocol must implement those methods. Protocols Can Have Optional Methodsīy default, all methods declared in a protocol are required methods. All that matters is that it conforms to the protocol, which means the pie chart view knows it can request the information it needs. It doesn’t matter whether the object is an instance of UIViewController or NSObject. Note: Delegate and data source properties are usually marked as weak for the object graph management reasons described earlier, in Avoid Strong Reference Cycles.īy specifying the required protocol conformance on the property, you’ll get a compiler warning if you attempt to set the property to an object that doesn’t conform to the protocol, even though the basic property class type is generic. The basic syntax to define a protocol looks like this: A protocol, by contrast, is used to declare methods and properties that are independent of any specific class. Protocols Define Messaging ContractsĪ class interface declares the methods and properties associated with that class. 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. Objective-C allows you to define protocols, which declare the methods expected to be used for a particular situation. In order for the table view to know whether an object is suitable as a data source, it’s important to be able to declare that the object implements the necessary methods. The data source could be an instance of any class, such as a view controller (a subclass of NSViewController on OS X or UIViewController on iOS) or a dedicated data source class that perhaps just inherits from NSObject. This means that the data source must respond to a specific set of messages that the table view might send. As an example, a table view expects to be able to communicate with a data source object in order to find out what it is required to display. In the world of object-oriented programming, it’s important to be able to define a set of behavior that is expected of an object in a given situation. Law enforcement officials, for example, are required to “follow protocol” when making enquiries or collecting evidence. In the real world, people on official business are often required to follow strict procedures when dealing with certain situations.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |