1.

What Is A Protocol, And How Do You Define Your Own And When Is It Used?

Answer»

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.

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

@optional

@end

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



Discussion

No Comment Found