Where to put my Struct to keep classes independent?












1















I have a class which I want to be reusable (for any developer user who wants it, or outside my project). This class returns a Struct lets call it structA, and the class is classA.



The first thought is to put structA inside the same file of this class, so while the Struct is public, any user who get this class file can read this Struct which the class returns.



Till here it looks reasonable. (or not)



But what happens if other classes in my current project also need to use this Struct for other purposes? (a copy of it)



Then to make the code reusable I need to put this struct somewhere else.
By reusable I mean there are no dependencies- get the class file and that is it.



If it's outside classA, then classA becomes not reusable and dependent.



If it's inside classA, then other class that use it are also dependent(not reusable outside)



The only solution left is to create other Structs like this with different names(sounds bad)



Where would you put this Struct in your Xcode project?










share|improve this question




















  • 1





    What is your proposed distribution method / format? Files on github? Pod? Framework? Library?

    – matt
    Nov 24 '18 at 6:44













  • Thanks, assuming I just want to write code more efficient , for example inside the project there is a group of 10 classes that creates a curve view. I would like to know that anyone who copy this folder at any point, can use this in his own project. So should I locate this structure in one of those 10 files ? seems reasonable, but the other classes in MY project also need to use it. I can't find a reasonable place for it (as dumb as this question might be)

    – Curnelious
    Nov 24 '18 at 7:12













  • Put the files in a folder? Then you can just copy the folder and everything goes with it.

    – Sweeper
    Nov 24 '18 at 8:49
















1















I have a class which I want to be reusable (for any developer user who wants it, or outside my project). This class returns a Struct lets call it structA, and the class is classA.



The first thought is to put structA inside the same file of this class, so while the Struct is public, any user who get this class file can read this Struct which the class returns.



Till here it looks reasonable. (or not)



But what happens if other classes in my current project also need to use this Struct for other purposes? (a copy of it)



Then to make the code reusable I need to put this struct somewhere else.
By reusable I mean there are no dependencies- get the class file and that is it.



If it's outside classA, then classA becomes not reusable and dependent.



If it's inside classA, then other class that use it are also dependent(not reusable outside)



The only solution left is to create other Structs like this with different names(sounds bad)



Where would you put this Struct in your Xcode project?










share|improve this question




















  • 1





    What is your proposed distribution method / format? Files on github? Pod? Framework? Library?

    – matt
    Nov 24 '18 at 6:44













  • Thanks, assuming I just want to write code more efficient , for example inside the project there is a group of 10 classes that creates a curve view. I would like to know that anyone who copy this folder at any point, can use this in his own project. So should I locate this structure in one of those 10 files ? seems reasonable, but the other classes in MY project also need to use it. I can't find a reasonable place for it (as dumb as this question might be)

    – Curnelious
    Nov 24 '18 at 7:12













  • Put the files in a folder? Then you can just copy the folder and everything goes with it.

    – Sweeper
    Nov 24 '18 at 8:49














1












1








1








I have a class which I want to be reusable (for any developer user who wants it, or outside my project). This class returns a Struct lets call it structA, and the class is classA.



The first thought is to put structA inside the same file of this class, so while the Struct is public, any user who get this class file can read this Struct which the class returns.



Till here it looks reasonable. (or not)



But what happens if other classes in my current project also need to use this Struct for other purposes? (a copy of it)



Then to make the code reusable I need to put this struct somewhere else.
By reusable I mean there are no dependencies- get the class file and that is it.



If it's outside classA, then classA becomes not reusable and dependent.



If it's inside classA, then other class that use it are also dependent(not reusable outside)



The only solution left is to create other Structs like this with different names(sounds bad)



Where would you put this Struct in your Xcode project?










share|improve this question
















I have a class which I want to be reusable (for any developer user who wants it, or outside my project). This class returns a Struct lets call it structA, and the class is classA.



The first thought is to put structA inside the same file of this class, so while the Struct is public, any user who get this class file can read this Struct which the class returns.



Till here it looks reasonable. (or not)



But what happens if other classes in my current project also need to use this Struct for other purposes? (a copy of it)



Then to make the code reusable I need to put this struct somewhere else.
By reusable I mean there are no dependencies- get the class file and that is it.



If it's outside classA, then classA becomes not reusable and dependent.



If it's inside classA, then other class that use it are also dependent(not reusable outside)



The only solution left is to create other Structs like this with different names(sounds bad)



Where would you put this Struct in your Xcode project?







swift struct






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 10:37









Damon

520318




520318










asked Nov 24 '18 at 6:19









CurneliousCurnelious

3,862750102




3,862750102








  • 1





    What is your proposed distribution method / format? Files on github? Pod? Framework? Library?

    – matt
    Nov 24 '18 at 6:44













  • Thanks, assuming I just want to write code more efficient , for example inside the project there is a group of 10 classes that creates a curve view. I would like to know that anyone who copy this folder at any point, can use this in his own project. So should I locate this structure in one of those 10 files ? seems reasonable, but the other classes in MY project also need to use it. I can't find a reasonable place for it (as dumb as this question might be)

    – Curnelious
    Nov 24 '18 at 7:12













  • Put the files in a folder? Then you can just copy the folder and everything goes with it.

    – Sweeper
    Nov 24 '18 at 8:49














  • 1





    What is your proposed distribution method / format? Files on github? Pod? Framework? Library?

    – matt
    Nov 24 '18 at 6:44













  • Thanks, assuming I just want to write code more efficient , for example inside the project there is a group of 10 classes that creates a curve view. I would like to know that anyone who copy this folder at any point, can use this in his own project. So should I locate this structure in one of those 10 files ? seems reasonable, but the other classes in MY project also need to use it. I can't find a reasonable place for it (as dumb as this question might be)

    – Curnelious
    Nov 24 '18 at 7:12













  • Put the files in a folder? Then you can just copy the folder and everything goes with it.

    – Sweeper
    Nov 24 '18 at 8:49








1




1





What is your proposed distribution method / format? Files on github? Pod? Framework? Library?

– matt
Nov 24 '18 at 6:44







What is your proposed distribution method / format? Files on github? Pod? Framework? Library?

– matt
Nov 24 '18 at 6:44















Thanks, assuming I just want to write code more efficient , for example inside the project there is a group of 10 classes that creates a curve view. I would like to know that anyone who copy this folder at any point, can use this in his own project. So should I locate this structure in one of those 10 files ? seems reasonable, but the other classes in MY project also need to use it. I can't find a reasonable place for it (as dumb as this question might be)

– Curnelious
Nov 24 '18 at 7:12







Thanks, assuming I just want to write code more efficient , for example inside the project there is a group of 10 classes that creates a curve view. I would like to know that anyone who copy this folder at any point, can use this in his own project. So should I locate this structure in one of those 10 files ? seems reasonable, but the other classes in MY project also need to use it. I can't find a reasonable place for it (as dumb as this question might be)

– Curnelious
Nov 24 '18 at 7:12















Put the files in a folder? Then you can just copy the folder and everything goes with it.

– Sweeper
Nov 24 '18 at 8:49





Put the files in a folder? Then you can just copy the folder and everything goes with it.

– Sweeper
Nov 24 '18 at 8:49












2 Answers
2






active

oldest

votes


















2














Put your struct in a separate file and document for each class you want to reuse that it depends on this struct. So basically you make your struct reusable as well.



The process of reusing your class becomes a little bit harder - you also have to include it’s dependencies. But that is just a fact of life, basically all software has dependencies. To make it easier to add some code dependency manages such as Cocoapods, Carthage or the Swift Package Manager have been invented. With those you specify for each library what it depends on - so ClassA depends on StructA. In the project you want to use ClassA you specify only that dependency and the package manager installs all of it’s dependencies (and their dependencies and so on) together.






share|improve this answer































    1














    You can create a separate library or framework which will contain all the reusable classes and structs.



    Whenever you want to use the class classA or the struct structA, you can just import that library in the respective file.



    Repeating the code is not recommended.






    share|improve this answer























      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%2f53455706%2fwhere-to-put-my-struct-to-keep-classes-independent%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









      2














      Put your struct in a separate file and document for each class you want to reuse that it depends on this struct. So basically you make your struct reusable as well.



      The process of reusing your class becomes a little bit harder - you also have to include it’s dependencies. But that is just a fact of life, basically all software has dependencies. To make it easier to add some code dependency manages such as Cocoapods, Carthage or the Swift Package Manager have been invented. With those you specify for each library what it depends on - so ClassA depends on StructA. In the project you want to use ClassA you specify only that dependency and the package manager installs all of it’s dependencies (and their dependencies and so on) together.






      share|improve this answer




























        2














        Put your struct in a separate file and document for each class you want to reuse that it depends on this struct. So basically you make your struct reusable as well.



        The process of reusing your class becomes a little bit harder - you also have to include it’s dependencies. But that is just a fact of life, basically all software has dependencies. To make it easier to add some code dependency manages such as Cocoapods, Carthage or the Swift Package Manager have been invented. With those you specify for each library what it depends on - so ClassA depends on StructA. In the project you want to use ClassA you specify only that dependency and the package manager installs all of it’s dependencies (and their dependencies and so on) together.






        share|improve this answer


























          2












          2








          2







          Put your struct in a separate file and document for each class you want to reuse that it depends on this struct. So basically you make your struct reusable as well.



          The process of reusing your class becomes a little bit harder - you also have to include it’s dependencies. But that is just a fact of life, basically all software has dependencies. To make it easier to add some code dependency manages such as Cocoapods, Carthage or the Swift Package Manager have been invented. With those you specify for each library what it depends on - so ClassA depends on StructA. In the project you want to use ClassA you specify only that dependency and the package manager installs all of it’s dependencies (and their dependencies and so on) together.






          share|improve this answer













          Put your struct in a separate file and document for each class you want to reuse that it depends on this struct. So basically you make your struct reusable as well.



          The process of reusing your class becomes a little bit harder - you also have to include it’s dependencies. But that is just a fact of life, basically all software has dependencies. To make it easier to add some code dependency manages such as Cocoapods, Carthage or the Swift Package Manager have been invented. With those you specify for each library what it depends on - so ClassA depends on StructA. In the project you want to use ClassA you specify only that dependency and the package manager installs all of it’s dependencies (and their dependencies and so on) together.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 8:50









          SvenSven

          20.4k44568




          20.4k44568

























              1














              You can create a separate library or framework which will contain all the reusable classes and structs.



              Whenever you want to use the class classA or the struct structA, you can just import that library in the respective file.



              Repeating the code is not recommended.






              share|improve this answer




























                1














                You can create a separate library or framework which will contain all the reusable classes and structs.



                Whenever you want to use the class classA or the struct structA, you can just import that library in the respective file.



                Repeating the code is not recommended.






                share|improve this answer


























                  1












                  1








                  1







                  You can create a separate library or framework which will contain all the reusable classes and structs.



                  Whenever you want to use the class classA or the struct structA, you can just import that library in the respective file.



                  Repeating the code is not recommended.






                  share|improve this answer













                  You can create a separate library or framework which will contain all the reusable classes and structs.



                  Whenever you want to use the class classA or the struct structA, you can just import that library in the respective file.



                  Repeating the code is not recommended.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 24 '18 at 7:51









                  DamonDamon

                  520318




                  520318






























                      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%2f53455706%2fwhere-to-put-my-struct-to-keep-classes-independent%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

                      404 Error Contact Form 7 ajax form submitting

                      How to know if a Active Directory user can login interactively

                      Refactoring coordinates for Minecraft Pi buildings written in Python