The Swift struct is a common and flexible construct for building code.
We can extend the functionality of a struct by defining attributes (constants, variables) and adding methods for the struct body.
Unlike C and Objective C:
Structs do not need to contain implementation files and interfaces.
Structs allow us to create a single file, and the system automatically generates external interfaces for other code.
The struct is passed in the code in the way it is copied, so its value is not modifiable.
Grammar
We define structs by the keyword struct:
struct namestruct { 1 2 ... Definition N}
Instance
We define a struct named Markstruct, which is a property of the student's three-account score, and the data type is INT:
struct markstruct{ var mark1:int var mark2:int var mark3:int}
Struct members can be accessed through struct names.
struct instantiation using the Let keyword:
struct Studentmarks {var mark1 = 100 var mark2 = 78
var mark3 = 98 }let marks = Studentmarks () print ( " mark1 is \ (marks.mark1) " ) print ( " mark2 Yes \ ( MARKS.MARK2) " ) print ( " mark3 is \ (MARKS.MARK3) " )
We access students ' scores through the structure name ' Studentmarks '. Struct members are initialized to Mark1, MARK2, MARK3, and the data type is integer.
We then instantiate the struct Studentmarks () by using the Let keyword and pass it to marks.
Finally, we go through the . number to access the value of the struct member.
The following instantiation passes a value and clones a struct by instantiating the struct:
struct marksstruct { var mark:int init (mark:int) { = Mark 98// astruct and bstruct are structs that use the same value! //98//
Application of structural Body
In your code, you can use structs to define your custom data types.
struct instances are always defined by value passing to define your custom data type.
As a general guideline, consider building a struct when one or more of the following conditions are met:
- The main purpose of the structure is to encapsulate a small number of related simple data values.
- It is reasonable to expect that when an instance of a struct is assigned or passed, the encapsulated data will be copied instead of referenced.
- Any value type attribute stored in the struct will also be copied, not referenced.
- Structs do not need to inherit the properties or behaviors of another existing type.
For example, the following scenarios are appropriate for the use of structs:
- The size of the geometry, encapsulating an
width
attribute and a height
property, both Double
types.
- A path within a range that encapsulates a
start
property and length
attribute, both of which are Int
types.
- In a three-dimensional coordinate system, a point, package
x
, y
and z
attribute, all of them are Double
types.
struct instances are passed by value rather than by reference.
structmarkstruct{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}} Print ("Excellent results:") var marks= Markstruct (mark1:98, MARK2: the, Mark3: -) print (MARKS.MARK1) print (MARKS.MARK2) print (MARKS.MARK3) print ("Bad results:") var fail= Markstruct (mark1: the, MARK2: the, Mark3: -) print (FAIL.MARK1) print (FAIL.MARK2) print (FAIL.MARK3)
Use of Swift structures