How can I return instancetype in Swift from instance method
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Consider method like this:
- (instancetype)sizeFit {
[self sizeToFit];
return self;
}
It’s useful for method chaining, I have whole api written like this in my framework and wanted to move it to Swift, searched here questions but found just some complicated answers instead of no, it’s not possible in normal readable way.
objective-c swift
add a comment |
Consider method like this:
- (instancetype)sizeFit {
[self sizeToFit];
return self;
}
It’s useful for method chaining, I have whole api written like this in my framework and wanted to move it to Swift, searched here questions but found just some complicated answers instead of no, it’s not possible in normal readable way.
objective-c swift
add a comment |
Consider method like this:
- (instancetype)sizeFit {
[self sizeToFit];
return self;
}
It’s useful for method chaining, I have whole api written like this in my framework and wanted to move it to Swift, searched here questions but found just some complicated answers instead of no, it’s not possible in normal readable way.
objective-c swift
Consider method like this:
- (instancetype)sizeFit {
[self sizeToFit];
return self;
}
It’s useful for method chaining, I have whole api written like this in my framework and wanted to move it to Swift, searched here questions but found just some complicated answers instead of no, it’s not possible in normal readable way.
objective-c swift
objective-c swift
edited Nov 23 '18 at 3:55
rmaddy
247k27329394
247k27329394
asked Nov 23 '18 at 2:19
RenetikRenetik
2,0912235
2,0912235
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
The very short answer is no there is no direct equivalent.
The more detailed answer is that the languages are conceptually different.
- In Objective-C anything returned is an object aka
id
. Usinginstancetype
is a hint to the compiler and IDE for a specific type to allow further checks and auto-completion. This works for sub-classing and extensions. - Swift is strongly typed, there is no base object and we also have classes and structs. A function can only return a specific type,
Self
, a generic type or anassociatedtype
. The best approximation would be to move this functionality into anextension
orprotocol
and useSelf
. This would work the same way. There is no equivalent for this in a sub classing context.
Long detailed answer Return instancetype in Swift
Ok I got it so extension can do it the same/similar way .... in sub-classing its not possible, thanks.
– Renetik
Nov 23 '18 at 15:58
add a comment |
Thanks, so in short, this is equivalent way in swift to return instance type:
extension UIView {
func someFunctionThatReturnInstanceTypeInAllSubclassesOfView() -> Self {
return self
}
}
It wont work in classes though.
That works well in extensions. One can see that for protocol extensions in the standard library.
– mhei
Nov 23 '18 at 22:28
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%2f53439923%2fhow-can-i-return-instancetype-in-swift-from-instance-method%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
The very short answer is no there is no direct equivalent.
The more detailed answer is that the languages are conceptually different.
- In Objective-C anything returned is an object aka
id
. Usinginstancetype
is a hint to the compiler and IDE for a specific type to allow further checks and auto-completion. This works for sub-classing and extensions. - Swift is strongly typed, there is no base object and we also have classes and structs. A function can only return a specific type,
Self
, a generic type or anassociatedtype
. The best approximation would be to move this functionality into anextension
orprotocol
and useSelf
. This would work the same way. There is no equivalent for this in a sub classing context.
Long detailed answer Return instancetype in Swift
Ok I got it so extension can do it the same/similar way .... in sub-classing its not possible, thanks.
– Renetik
Nov 23 '18 at 15:58
add a comment |
The very short answer is no there is no direct equivalent.
The more detailed answer is that the languages are conceptually different.
- In Objective-C anything returned is an object aka
id
. Usinginstancetype
is a hint to the compiler and IDE for a specific type to allow further checks and auto-completion. This works for sub-classing and extensions. - Swift is strongly typed, there is no base object and we also have classes and structs. A function can only return a specific type,
Self
, a generic type or anassociatedtype
. The best approximation would be to move this functionality into anextension
orprotocol
and useSelf
. This would work the same way. There is no equivalent for this in a sub classing context.
Long detailed answer Return instancetype in Swift
Ok I got it so extension can do it the same/similar way .... in sub-classing its not possible, thanks.
– Renetik
Nov 23 '18 at 15:58
add a comment |
The very short answer is no there is no direct equivalent.
The more detailed answer is that the languages are conceptually different.
- In Objective-C anything returned is an object aka
id
. Usinginstancetype
is a hint to the compiler and IDE for a specific type to allow further checks and auto-completion. This works for sub-classing and extensions. - Swift is strongly typed, there is no base object and we also have classes and structs. A function can only return a specific type,
Self
, a generic type or anassociatedtype
. The best approximation would be to move this functionality into anextension
orprotocol
and useSelf
. This would work the same way. There is no equivalent for this in a sub classing context.
Long detailed answer Return instancetype in Swift
The very short answer is no there is no direct equivalent.
The more detailed answer is that the languages are conceptually different.
- In Objective-C anything returned is an object aka
id
. Usinginstancetype
is a hint to the compiler and IDE for a specific type to allow further checks and auto-completion. This works for sub-classing and extensions. - Swift is strongly typed, there is no base object and we also have classes and structs. A function can only return a specific type,
Self
, a generic type or anassociatedtype
. The best approximation would be to move this functionality into anextension
orprotocol
and useSelf
. This would work the same way. There is no equivalent for this in a sub classing context.
Long detailed answer Return instancetype in Swift
answered Nov 23 '18 at 7:56
mheimhei
663
663
Ok I got it so extension can do it the same/similar way .... in sub-classing its not possible, thanks.
– Renetik
Nov 23 '18 at 15:58
add a comment |
Ok I got it so extension can do it the same/similar way .... in sub-classing its not possible, thanks.
– Renetik
Nov 23 '18 at 15:58
Ok I got it so extension can do it the same/similar way .... in sub-classing its not possible, thanks.
– Renetik
Nov 23 '18 at 15:58
Ok I got it so extension can do it the same/similar way .... in sub-classing its not possible, thanks.
– Renetik
Nov 23 '18 at 15:58
add a comment |
Thanks, so in short, this is equivalent way in swift to return instance type:
extension UIView {
func someFunctionThatReturnInstanceTypeInAllSubclassesOfView() -> Self {
return self
}
}
It wont work in classes though.
That works well in extensions. One can see that for protocol extensions in the standard library.
– mhei
Nov 23 '18 at 22:28
add a comment |
Thanks, so in short, this is equivalent way in swift to return instance type:
extension UIView {
func someFunctionThatReturnInstanceTypeInAllSubclassesOfView() -> Self {
return self
}
}
It wont work in classes though.
That works well in extensions. One can see that for protocol extensions in the standard library.
– mhei
Nov 23 '18 at 22:28
add a comment |
Thanks, so in short, this is equivalent way in swift to return instance type:
extension UIView {
func someFunctionThatReturnInstanceTypeInAllSubclassesOfView() -> Self {
return self
}
}
It wont work in classes though.
Thanks, so in short, this is equivalent way in swift to return instance type:
extension UIView {
func someFunctionThatReturnInstanceTypeInAllSubclassesOfView() -> Self {
return self
}
}
It wont work in classes though.
answered Nov 23 '18 at 16:03
RenetikRenetik
2,0912235
2,0912235
That works well in extensions. One can see that for protocol extensions in the standard library.
– mhei
Nov 23 '18 at 22:28
add a comment |
That works well in extensions. One can see that for protocol extensions in the standard library.
– mhei
Nov 23 '18 at 22:28
That works well in extensions. One can see that for protocol extensions in the standard library.
– mhei
Nov 23 '18 at 22:28
That works well in extensions. One can see that for protocol extensions in the standard library.
– mhei
Nov 23 '18 at 22:28
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%2f53439923%2fhow-can-i-return-instancetype-in-swift-from-instance-method%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