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;
}







0















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.










share|improve this question































    0















    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.










    share|improve this question



























      0












      0








      0








      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.










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 3:55









      rmaddy

      247k27329394




      247k27329394










      asked Nov 23 '18 at 2:19









      RenetikRenetik

      2,0912235




      2,0912235
























          2 Answers
          2






          active

          oldest

          votes


















          1














          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. Using instancetype 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 an associatedtype. The best approximation would be to move this functionality into an extension or protocol and use Self. This would work the same way. There is no equivalent for this in a sub classing context.


          Long detailed answer Return instancetype in Swift






          share|improve this answer
























          • 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



















          0














          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.






          share|improve this answer
























          • That works well in extensions. One can see that for protocol extensions in the standard library.

            – mhei
            Nov 23 '18 at 22:28












          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          1














          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. Using instancetype 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 an associatedtype. The best approximation would be to move this functionality into an extension or protocol and use Self. This would work the same way. There is no equivalent for this in a sub classing context.


          Long detailed answer Return instancetype in Swift






          share|improve this answer
























          • 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
















          1














          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. Using instancetype 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 an associatedtype. The best approximation would be to move this functionality into an extension or protocol and use Self. This would work the same way. There is no equivalent for this in a sub classing context.


          Long detailed answer Return instancetype in Swift






          share|improve this answer
























          • 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














          1












          1








          1







          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. Using instancetype 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 an associatedtype. The best approximation would be to move this functionality into an extension or protocol and use Self. This would work the same way. There is no equivalent for this in a sub classing context.


          Long detailed answer Return instancetype in Swift






          share|improve this answer













          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. Using instancetype 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 an associatedtype. The best approximation would be to move this functionality into an extension or protocol and use Self. This would work the same way. There is no equivalent for this in a sub classing context.


          Long detailed answer Return instancetype in Swift







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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



















          • 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













          0














          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.






          share|improve this answer
























          • That works well in extensions. One can see that for protocol extensions in the standard library.

            – mhei
            Nov 23 '18 at 22:28
















          0














          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.






          share|improve this answer
























          • That works well in extensions. One can see that for protocol extensions in the standard library.

            – mhei
            Nov 23 '18 at 22:28














          0












          0








          0







          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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



















          • 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


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents