Explain types of protocol in Objective-C
Explain types of protocol in Objective-C
In Objective-C, protocols are used to define a contract between classes or structs, specifying a set of methods and properties that must be implemented by the adopting class or struct. There are two main types of protocols in Objective-C:
Formal protocols are the most commonly used type of protocols in Objective-C. They are defined using the @protocol
keyword and can include method declarations, property declarations, and even optional methods and properties. Formal protocols are adopted by classes using the <>
syntax, and the adopting class must implement all the required methods and properties defined in the protocol.
Example:
@protocol MyProtocol
- (void)doSomething;
@property (nonatomic, copy) NSString *name;
@end
Informal protocols, also known as "method categories," are defined using method categories in a class extension o...
junior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào