Hello,
I have what I think are 2 simple (noob level) questions.
The First Question
lets see if I am understanding this correctly.
1) I declare / create 1 Class and 1 Struct
2) Within the Class I Instantiate the Struct
3) Later, all references to the Class are removed
4) Swift removes the class and frees up the resources from the Class, as well as the copy of the struct that I instantiated within the class.
So Hypothetically speaking if the Class (without the Struct) consumed 2KB of memory, and the Struct by itself consumed 3KB of memory. (so the Class with the Struct in it consumed 5KB of memory total) When all references to the class are gone, then the system will clear up 5KB of memory right?
Very quick example: (I'm going to omit things like init(){}, for the sake of keeping this simple at to the point of my question)
The Second Question
I "think" I have a basic grasp on the differences in a Struct and a Class, but the best time to use each still feels fuzzy. (Struct is a Value type which means a new copy is made every time yo instantiate one, a Class is a reference type which means only the first is copied, then any other instantiations just create references to the class...right?)
Some basic practical examples might help my brain.
If I have a game that is spawning bad guys to fight. I'm assuming those bad guys would be structs since they each have their own stats as opposed to sharing 1 pool of stats is that right? (If I hit enemy #2 I don't want them referencing the same health level, I want them to each have their own copy) is that right?
But perhaps level end bosses might use a class because there is only ever 1 at any time, so as long as the Boss Class stats are reset / adjusted appropriately then a class would be a good fit.
I'm sure these are not the best examples ever, but I really am just trying to get my head wrapped around it properly.
Thanks!
~Ark
I have what I think are 2 simple (noob level) questions.
The First Question
lets see if I am understanding this correctly.
1) I declare / create 1 Class and 1 Struct
2) Within the Class I Instantiate the Struct
3) Later, all references to the Class are removed
4) Swift removes the class and frees up the resources from the Class, as well as the copy of the struct that I instantiated within the class.
So Hypothetically speaking if the Class (without the Struct) consumed 2KB of memory, and the Struct by itself consumed 3KB of memory. (so the Class with the Struct in it consumed 5KB of memory total) When all references to the class are gone, then the system will clear up 5KB of memory right?
Very quick example: (I'm going to omit things like init(){}, for the sake of keeping this simple at to the point of my question)
Code:
struct PlayerStats{ var pName = "MyName"; var pLife = 100.0; [More Code] }
class Players { var MyStats = PlayerStats(); [ More Code] } // Class Players now contains a copy of struct PlaterStats in a variable called "MyStats"
var player1 = Players() //Instantiate
[More Code]
player1 = nil // no more references to the class "Players" so all resources including the copy of PlayerStats (in Players() called MyStats) are removed from memory.
The Second Question
I "think" I have a basic grasp on the differences in a Struct and a Class, but the best time to use each still feels fuzzy. (Struct is a Value type which means a new copy is made every time yo instantiate one, a Class is a reference type which means only the first is copied, then any other instantiations just create references to the class...right?)
Some basic practical examples might help my brain.
If I have a game that is spawning bad guys to fight. I'm assuming those bad guys would be structs since they each have their own stats as opposed to sharing 1 pool of stats is that right? (If I hit enemy #2 I don't want them referencing the same health level, I want them to each have their own copy) is that right?
But perhaps level end bosses might use a class because there is only ever 1 at any time, so as long as the Boss Class stats are reset / adjusted appropriately then a class would be a good fit.
I'm sure these are not the best examples ever, but I really am just trying to get my head wrapped around it properly.
Thanks!
~Ark
Last edited: