Swift 4 provides a flexible building block of making use of constructs as Structures. By making use of these structures once can define constructs methods and properties.
Unlike C and Objective C
- Structure need not require implementation files and interface.
- Structure allows us to create a single file and to extend its interface automatically to other blocks.
In Structure the variable values are copied and passed in subsequent codes by returning a copy of the old values so that the values cannot be altered.
Syntax
Structures are defined with a 'Struct' Keyword. struct nameStruct { Definition 1 Definition 2 --- Definition N }
Definition of a Structure
Consider for example, suppose we have to access students record containing marks of three subjects and to find out the total of three subjects. Here markStruct is used to initialize a structure with three marks as datatype 'Int'.
struct MarkStruct { var mark1: Int var mark2: Int var mark3: Int }
Accessing the Structure and its Properties
The members of the structure are accessed by its structure name. The instances of the structure are initialized by the 'let' keyword.
Live Demostruct studentMarks { var mark1 = 100 var mark2 = 200 var mark3 = 300 } let marks = studentMarks() print("Mark1 is \(marks.mark1)") print("Mark2 is \(marks.mark2)") print("Mark3 is \(marks.mark3)")
When we run the above program using playground, we get the following result −
Mark1 is 100 Mark2 is 200 Mark3 is 300
Students marks are accessed by the structure name 'studentMarks'. The structure members are initialized as mark1, mark2, mark3 with integer type values. Then the structure studentMarks() is passed to the 'marks' with 'let' keyword. Hereafter 'marks' will contain the structure member values. Now the values are printed by accessing the structure member values by '.' with its initialized names.
Live Demostruct MarksStruct { var mark: Int init(mark: Int) { self.mark = mark } } var aStruct = MarksStruct(mark: 98) var bStruct = aStruct // aStruct and bStruct are two structs with the same value! bStruct.mark = 97 print(aStruct.mark) // 98 print(bStruct.mark) // 97
When we run the above program using playground, we get the following result −
98 97
Best Usage Practices of Structures
Swift 4 language provides the functionality to define structures as custom data types for building the function blocks. The instances of structure are passed by its value to the defined blocks for further manipulations.
Need for having structures
- To encapsulate simple data values.
- To copy the encapsulated data and its associated properties by 'values' rather than by 'references'.
- Structure to 'Copy' and 'Reference'.
Structures in Swift 4 pass their members with their values rather than by its references.
Live Demostruct markStruct { var mark1: Int var mark2: Int var mark3: Int init(mark1: Int, mark2: Int, mark3: Int) { self.mark1 = mark1 self.mark2 = mark2 self.mark3 = mark3 } } var marks = markStruct(mark1: 98, mark2: 96, mark3:100) print(marks.mark1) print(marks.mark2) print(marks.mark3)
When we run the above program using playground, we get the following result −
98 96 100
Another Example
Live Demostruct markStruct { var mark1: Int var mark2: Int var mark3: Int init(mark1: Int, mark2: Int, mark3: Int) { self.mark1 = mark1 self.mark2 = mark2 self.mark3 = mark3 } } var fail = markStruct(mark1: 34, mark2: 42, mark3: 13) print(fail.mark1) print(fail.mark2) print(fail.mark3)
When we run the above program using playground, we get the following result −
34 42 13
The structure 'markStruct' is defined first with its members mark1, mark2 and mark3. Now the variables of member classes are initialized to hold integer values. Then the copy of the structure members are created with 'self' Keyword. Once the copy of the structure members are created structure block with its parameter marks are passed to 'marks' variable which will now hold the students marks. Then the marks are printed as 98, 96, 100. Next step for the same structure members another instance named 'fail' is used to point the same structure members with different marks. Then the results are now printed as 34, 42, 13. This clearly explains that structures will have a copy of the member variables then pass the members to their upcoming function blocks.
No comments:
Post a Comment