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:

objc Copy
@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:

objc Copy
@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:

objc Copy
@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 is Dynamic Dispatch and how does it work in Objective-C?

middle

What's the difference between the atomic and nonatomic attributes?

expert

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

Bình luận

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

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