In this tutorial you will learn about the Swift Structures and its application with practical example.
Swift Structures
Structure is a user-defined data type, that encapsulate the properties (variables and constant) and methods(functions). It is named type, you can define and five it a name and later you can use it in your. In Swift, Classes and structures are much similar, but one key differences is that classes are reference and structures are value types.
Unlike many other programming languages where structure are mostly used to group related data together, swift structures are fully feature rich and you are allowed to do following things only possible with class –
properties
instance methods
type methods
initializers
subscripts
extensions
protocols
Defining a Structure In Swift
In Swift, structure can be defined using the struct keyword as following –
Syntax:-
1 2 3 4 |
struct structName{ // Properties // Methods } |
Here, structName is replaced with the actual structure name then in between the curly brackets {} we define all the properties and methods associated with the structure.
Example:-
1 2 3 4 5 |
struct employee { var empName: String var empAge: Int var empSalary: Int } |
Swift Structure Instances
Once a structure has been defined, we can create instance or objects of that structure which has access to structure properties and methods. In swift, an instance of a structure can be created as following –
Syntax:-
1 |
var instName = structName() |
Here, instName and structName is replaced with actual instance name and the structure name respectively.
Example:-
1 2 3 4 5 6 7 8 9 |
struct employee { var empName: String = "John" var empAge: Int = 35 var empSalary: Int = 5000 } let emp = employee() print("W3Adda - Swift Structure and Instance") print("Salary of \(emp.empName) is \(emp.empSalary)") |
Output:-
Swift Accessing Instance variable and methods
In swift once we have got an instance of a structure created, we can access properties and method of that structure using property/method name separated by a dot (.) operator after the instance name as following.
Syntax for Property:-
1 |
objectName.propName |
Syntax for Method:-
1 |
objectName.methodName() |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
struct employee { var empName: String = "John" var empAge: Int = 35 var empSalary: Int = 5000 func showEmpName(){ print("Employee name is : \(emp.empName)") } } var emp = employee() var empSalary:Int = emp.empSalary print("W3Adda - Swift Access Structure Property and Method") emp.showEmpName() print("Salary of \(emp.empName) is \(empSalary)") |
Output:-
Swift Structure Initializers
An initializer is a special kind of function in swift, it is used to provide initial values to structure properties while an instance of structure is created.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
struct employee { var empName: String = "John" var empAge: Int = 35 var empSalary: Int = 5000 init(empName: String, empAge: Int, empSalary: Int) { self.empName = empName self.empAge = empAge self.empSalary = empSalary } } var emp = employee(empName: "Keith", empAge: 25, empSalary: 4000) print("W3Adda - Swift Structure Initializers") print("Employee Name is :\(emp.empName)") print("Employee Age is :\(emp.empAge)") print("Employee Salary is :\(emp.empSalary)") |
Output:-
Swift Structures are Value Type
In swift class are of the value types, which means when we assign it to any constants, variables or function an individual copy of structure is created for each of the instance.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
struct employee { var empName : String init(empName: String) { self.empName = empName } } var emp1 = employee(empName: "Alex") var emp2 = emp1 emp2.empName = "John" print("W3Adda - Swift structures are value type") print(emp1.empName) print(emp2.empName) |
Here, in the above example we defined a structure employee, we created an instance emp1 with initial value “Alex” for empName property, then we have created another instance emp2 by assigning it previous instance emp1 and updated the value of empName property for emp2 instance, now we printed the value of empName property for both the instances and see that both the instance having individual copy of property.
Output:-
Difference between Classes and Structures
- Main difference between a class and structure is that classes are reference type and structures are value types.
- Structures does not provide deinitializers
- Structures does not provide default mutation
- Structures does not support type casting