@objc动态变量在Swift 4中是什么意思 [英] What does @objc dynamic var mean in Swift 4
问题描述
您能否简要解释一下使用Xcode 9.x的Swift 4中 @objc
和 dynamic
的含义?
Could you briefly explain what @objc
and dynamic
mean in Swift 4 using Xcode 9.x?
通过尝试和错误以及stackoverflow中的后续文章,我最终实现了该代码段。但我想对这些神奇的关键字有所了解。
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)
}
}
}
推荐答案
在Xcode及其反汇编程序的乐趣下,我发现了一些东西。感谢Mike Henderson的评论。
Having fun with Xcode and its disassembler, I have found some. Thanks to Mike Henderson's comment.
首先,添加 @objc
修饰符似乎使编译器编写了相应的符号可执行文件和/或库文件的 __ OBJC
段中的名称,然后将由Objective-C运行时系统使用。
otool -o filename
命令向我们显示 __ OBJC
段的内容。
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.
其次,添加 dynamic
修饰符似乎使编译器插入其他汇编程序代码以与Objective-C运行时系统进行交互。附加代码实现了通过 objc_msgSend()
及其相关功能来访问动态属性。同样,调用 dynamic
方法也将通过 objc_msgSend()
完成。
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()
.
现在,按照我的理解,行话动态调度
意味着使用 objc_msgSend()
,而静态派发
不使用它。在后一种情况下,访问变量和调用函数都将在没有Objective-C运行时系统干预的情况下完成,这与C ++ ABI的相似但不完全相同。
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.
显然,静态一个比动态一个快。但是,静态的无法实现Objective-C的神奇优势。借助Swift语言,我们有机会通过根据情况选择静态或动态调度,分别省略或添加那些神奇的关键字来利用这两个方面。
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.
谢谢!
更多阅读内容:
- Objective-C Runtime
- Using Swift with Cocoa and Objective-C (Swift 4.0.3)
这篇关于@objc动态变量在Swift 4中是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!