Where to put my Struct to keep classes independent?
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
add a comment |
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
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
add a comment |
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
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
swift struct
edited Nov 24 '18 at 10:37
Damon
5201318
5201318
asked Nov 24 '18 at 6:19
CurneliousCurnelious
3,879751103
3,879751103
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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.
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%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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 24 '18 at 8:50
SvenSven
20.4k44568
20.4k44568
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 24 '18 at 7:51
DamonDamon
5201318
5201318
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%2f53455706%2fwhere-to-put-my-struct-to-keep-classes-independent%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
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