Class And Structure in swift are good choices for storing data and modeling behavior in your apps, but their similarities can make it difficult to choose one over the other.
Consider the following recommendations to help choose which option makes sense when adding a new data type to your app.
- Structures use by default.
- Classes use when you need Objective-C interoperability.
- Add classes when you need to control the identity of the data you’re modeling.
- Using structures along with protocols to adopt behavior by sharing implementations.
Class And Structure in swift are general-purpose, flexible constructs that become the building blocks of your program’s code. You define properties and methods to add functionality to your structures and classes using the same syntax you use to define constants, variables, and functions.
Comparing Structures and Classes
Structures and classes in Swift have many things in common. Both can:
- Adding properties to store values
- Methods Define to provide functionality
- Subscripts Define to provide access to their values using subscript syntax
- Define initializers to set up their initial state
- Be extended to expand their functionality beyond a default implementation
- Conform to protocols to provide standard functionality of a certain kind
Classes have additional capabilities that structures don’t have:
- Inheritance enables one class to inherit the characteristics of another.
- Typecasting enables you to check and interpret the type of a class instance at runtime.
- Deinitializers enable an instance of a class to free up any resources it has assigned.
- Reference counting allows more than one reference to a class instance
Since a class is a reference type and it supports inheritance, the complexity increases. In most cases, a struct should be enough to meet your needs. Use a class when they’re appropriate or necessary
Design Class
To demonstrate the difference between classes
and structs
, first design a class, HumanClass
that has a name
property.
1 2 3 4 5 |
class TestClass { name: String init(name: String) { self.name = name } } |
Create Instance
1 2 |
var ClassObject = TestClass(name: "Bob") ClassObject.name // "Bob" |
Create another instance that “copies” humanClassObject
1 |
let newClassObject = ClassObject |
Change the name
property of newHumanClassObject
to “Bobby”.
1 2 |
ClassObject.name = "Bobby" newClassObject.name // "Bobby" |
The name
property of newHumanClassObject
has been changed to “Bobby” as well.
Conclusion
Thanks for reading this blog, this will help you to add the class or structure in your project and You can know more from here.
So pls follow the above step and And if you have any issue or suggestion you can leave your message in the comment section I will try to solve this.
Struct Design
Let us find out if the same behavior occurs with an object created with
structs
Create Instance
Create another instance that “copies”
structObject
Change the
name
property ofstructObject
to “Bobby”.On the contrary, the change in the
name
property ofstructObject
has no effect onnewStructObject.name
.The graph below shows the fundamental difference between
value types
vsreference types
.