+2 votes
in FuelPHP by

What is a protocol, and how do you define your own and when is it used?

1 Answer

0 votes
by

A protocol is similar to an interface from Java. It defines a list of required and optional methods that a class must/can implement if it adopts the protocol. Any class can implement a protocol and other classes can then send messages to that class based on the protocol methods without it knowing the type of the class.

@protocol MyCustomDataSource
- (NSUInteger)numberOfRecords;
- (NSDictionary *)recordAtIndex:(NSUInteger)index;
@optional
- (NSString *)titleForRecordAtIndex:(NSUInteger)index;
@end

A common use case is providing a DataSource for UITableView or UICollectionView.

Related questions

+2 votes
asked Jan 16, 2022 in FuelPHP by DavidAnderson
+2 votes
asked Jan 16, 2022 in FuelPHP by DavidAnderson
...