Câu hỏi phỏng vấn Objective-C
Câu hỏi

Explain types of protocol in Objective-C

Câu trả lời

Types of Protocols 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:

1. Formal Protocols

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:

objc Copy
@protocol MyProtocol
- (void)doSomething;
@property (nonatomic, copy) NSString *name;
@end

2. Informal Protocols

Informal protocols, also known as "method categories," are defined using method categories in a class extension o...

junior

junior

Gợi ý câu hỏi phỏng vấn

middle

When would you use Categories over Inheritance and vice versa?

expert

Explain the use case when ARC won't help you to release memory (but GC will)

senior

What happens when you invoke a method on a nil pointer?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào