Explain the difference between interfaces...
Explain the difference between interfaces...
Here is an explanation of the differences between interfaces, delegates, and protocols in Objective-C:
In Objective-C, an interface is a contract between a class and the outside world. It defines the methods and properties that a class exposes to other parts of the program. Interfaces are declared using the @interface
keyword followed by the class name. For example:
@interface MyClass : NSObject
- (void)doSomething;
@property (nonatomic, copy) NSString *name;
@end
This declares a class named MyClass
that inherits from NSObject
and has a method doSomething
and a property name
.
A protocol in Objective-C defines a blueprint of methods and properties that can be adopted by classes, categories, or class extensions. Protocols are declared using the @protocol
keyword followed by the protocol name. For example:
@protocol MyProtocol
- (void)protocolMethod;
@optional
- (void)optionalMethod;
@end
This declares a protocol named MyProtocol
with a required method protocolMethod
and an optional method optionalMethod
. Classes can adopt a protocol using the <>
syntax:
@interface MyClass : NSObject <MyProtocol>
// ...
@end
A delegate in Objective-C is an object that acts on behalf of another object. It is a way to implement the ...
senior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào