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

What's the difference between marking a method as @o...

Câu trả lời

The main difference between marking a method as @objc vs dynamic in Swift is:

  • @objc exposes the method to the Objective-C runtime, allowing it to be called from Objective-C code[1][5]. This is required for features like key-value observing to work correctly[1].

  • dynamic tells the Swift compiler to always use Objective-C dynamic dispatch for the method, rather than static or virtual dispatch[1][2]. This is necessary if you plan to modify the method implementation at runtime using the Objective-C runtime[2].

In Swift 4 and later, dynamic no longer implies @objc - you must explicitly use @objc dynamic to get both Objective-C visibility and dynamic dispatch[2][3].

You would use @objc when:

  • Interacting with Objective-C frameworks and APIs that require Objective-C visibility[5]
  • Using Objective-C features like selectors...
expert

expert

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

expert

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

senior

What are the differences between implementing a @property with @dynamic or @synthesize ?

expert

Which is the best of GCD, NSThread or NSOperationQueue ?

Bình luận

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

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