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

What is Dynamic Dispatch and h...

Câu trả lời

Dynamic dispatch is a mechanism in Objective-C that allows the specific implementation of a method to be determined at runtime, rather than at compile time. It enables method overriding, which is a key feature of runtime polymorphism in Objective-C.

Here's how dynamic dispatch works in Objective-C:

  1. When an object receives a message (method call), Objective-C's runtime system looks up the method implementation to be executed.

  2. The runtime system uses the object's class to determine the method implementation. It searches the class and its superclasses for a matching method.

  3. If the method is not found in the class hierarchy, the runtime system checks if the class has a custom implementation of the method resolution process using the resolveInstanceMethod: or resolveClassMethod: methods.

  4. If the method is still not found, the runtime system checks if the class has a forwarding implementation using the forwardInvocation: method. This allows the class to forward the message to another object.

  5. If none of the above steps resolve the method, the runtime system raises an exception.

Dynamic dispatch allows subclasses to override methods defined in their superclasses. When a method is overridden, the runtime system will execute the implementation provided by the most specific subclass ...

expert

expert

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

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

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