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

Explain the difference between interfaces...

Câu trả lời

Here is an explanation of the differences between interfaces, delegates, and protocols in Objective-C:

Interfaces

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.

Protocols

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

Delegates

A delegate in Objective-C is an object that acts on behalf of another object. It is a way to implement the ...

senior

senior

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

expert

What's the difference between marking a method as @objc vs dynamic , when would you do one vs the
other?

middle

A grandparent, parent and child problem

expert

What is Dynamic Dispatch and how does it work in Objective-C?

Bình luận

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

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