What does @objc dynamic var mean in Swift 4

空扰寡人 提交于 2020-01-01 09:12:11

问题


Could you briefly explain what @objc and dynamic mean in Swift 4 using Xcode 9.x?

With tries and errors and following articles in the stackoverflow, I have eventually achieved this snippet to work. But I would like to know a little bit about those magical keywords.

class SampleViewController: NSViewController {

  @objc class Parameters : NSObject {
    @objc dynamic var value1: Double = 0  // bound to Value of a NSTextfield with NumberFormatter
    @objc dynamic var value2: Double = 0  // as "parameters.value1" for the Model Key Path
  }

  @objc dynamic var parameters = Parameters()

  @objc dynamic var value3: Double {  // in the similar way as "value3" for the Model Key Path
    get {
      return parameters.value1 + parameters.value2
    }
  }

  override class func keyPathsForValuesAffectingValue(forKey key: String) -> Set<String> {
    switch key {
    case "value3" :
      return Set(["parameters.value1", "parameters.value2"])
    default:
      return super.keyPathsForValuesAffectingValue(forKey: key)
    }
  }

}

回答1:


Having fun with Xcode and its disassembler, I have found some. Thanks to Mike Henderson's comment.

Firstly, adding a @objc modifier seems to have the compiler write its corresponding symbol name in a __OBJC segment of executables and/or library files, which will be then used by the Objective-C run-time system. otool -o filename command shows us the contents of __OBJC segment.

Secondly, adding a dynamic modifier seems to have the compiler insert additional assembler codes to interact with the Objective-C run-time system. The additional code realizes that accessing dynamic properties will be done through objc_msgSend() and its related functions. Similarly, calling dynamic methods also will be done through objc_msgSend().

Now, in my understandings, the jargon dynamic dispatch implies use of objc_msgSend() while static dispatch does no use of it. In the latter case, both accessing variables and calling functions will be done without intervention of the Objective-C run-time system, which is in the similar, but not exactly same, way of C++ ABI.

Apparently, static one is faster than dynamic one. But static one is incapable of Objective-C's magical benefits, though. With the programming language Swift, we have opportunities to utilize both aspects by choosing either static or dynamic dispatch depending on the situation, by omitting or adding those magical keywords, respectively.

Thanks!

Further readings:

  • Objective-C Runtime
  • Using Swift with Cocoa and Objective-C (Swift 4.0.3)



回答2:


@objc means you want your Swift code (class, method, property, etc.) to be visible from Objective-C.

dynamic means you want to use Objective-C dynamic dispatch.

Swift 3 - dynamic vs @objc



来源:https://stackoverflow.com/questions/48559768/what-does-objc-dynamic-var-mean-in-swift-4

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!