What does @objc dynamic var mean in Swift 4
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)
}
}
}
dynamic swift4
add a comment |
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)
}
}
}
dynamic swift4
2
@objc
exposes the variable to the ObjC runtime.dynamic
tells the runtime to use dynamic dispatch instead of the default static dispatch.dynamic
also implies@objc
so@objc dynamic
is redundant. You mostly utilize them in KVO and Cocoa Binding. See this article: krakendev.io/blog/hipster-swift#dynamic
– Mike Henderson
Feb 1 '18 at 13:33
add a comment |
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)
}
}
}
dynamic swift4
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)
}
}
}
dynamic swift4
dynamic swift4
edited Feb 1 '18 at 17:25
rmaddy
244k27323384
244k27323384
asked Feb 1 '18 at 9:52
ToraTora
41759
41759
2
@objc
exposes the variable to the ObjC runtime.dynamic
tells the runtime to use dynamic dispatch instead of the default static dispatch.dynamic
also implies@objc
so@objc dynamic
is redundant. You mostly utilize them in KVO and Cocoa Binding. See this article: krakendev.io/blog/hipster-swift#dynamic
– Mike Henderson
Feb 1 '18 at 13:33
add a comment |
2
@objc
exposes the variable to the ObjC runtime.dynamic
tells the runtime to use dynamic dispatch instead of the default static dispatch.dynamic
also implies@objc
so@objc dynamic
is redundant. You mostly utilize them in KVO and Cocoa Binding. See this article: krakendev.io/blog/hipster-swift#dynamic
– Mike Henderson
Feb 1 '18 at 13:33
2
2
@objc
exposes the variable to the ObjC runtime. dynamic
tells the runtime to use dynamic dispatch instead of the default static dispatch. dynamic
also implies @objc
so @objc dynamic
is redundant. You mostly utilize them in KVO and Cocoa Binding. See this article: krakendev.io/blog/hipster-swift#dynamic– Mike Henderson
Feb 1 '18 at 13:33
@objc
exposes the variable to the ObjC runtime. dynamic
tells the runtime to use dynamic dispatch instead of the default static dispatch. dynamic
also implies @objc
so @objc dynamic
is redundant. You mostly utilize them in KVO and Cocoa Binding. See this article: krakendev.io/blog/hipster-swift#dynamic– Mike Henderson
Feb 1 '18 at 13:33
add a comment |
2 Answers
2
active
oldest
votes
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)
add a comment |
@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
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48559768%2fwhat-does-objc-dynamic-var-mean-in-swift-4%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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)
add a comment |
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)
add a comment |
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)
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)
answered Feb 4 '18 at 10:58
ToraTora
41759
41759
add a comment |
add a comment |
@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
add a comment |
@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
add a comment |
@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
@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
answered Jan 2 at 8:05
Gagandeep GambhirGagandeep Gambhir
919817
919817
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48559768%2fwhat-does-objc-dynamic-var-mean-in-swift-4%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
@objc
exposes the variable to the ObjC runtime.dynamic
tells the runtime to use dynamic dispatch instead of the default static dispatch.dynamic
also implies@objc
so@objc dynamic
is redundant. You mostly utilize them in KVO and Cocoa Binding. See this article: krakendev.io/blog/hipster-swift#dynamic– Mike Henderson
Feb 1 '18 at 13:33