Swift Structures
π
2026-06-19 | π Swift
Swift structures are a general-purpose and flexible construct used for building code.\\n\\nWe can define properties (constants, variables) and add methods to structures, thereby extending their functionality.\\n\\nUnlike C and Objective-C:\\n\\n* Structures do not need to include implementation files and interfaces.\\n* Structures allow us to create a single file, and the system automatically generates an external interface for other code.\\n\\nStructures are always passed in code by being copied, so their values are immutable.\\n\\n### Syntax\\n\\nWe define structures using the struct keyword:\\n\\nstruct nameStruct { Definition 1 Definition 2 β¦β¦ Definition N }\\n\\n* * *\\n\\n### Instance\\n\\nWe define a structure named MarkStruct, the properties of the structure are the scores of a student's three subjects, and the data type is Int:\\n\\nstruct MarkStruct{ var mark1: Int var mark2: Int var mark3: Int}\\nWe can access structure members through the structure name.\\n\\nStructure instantiation uses the let keyword:\\n\\nimport Cocoastruct studentMarks { var mark1 = 100 var mark2 = 78 var mark3 = 98}let marks = studentMarks()print("Mark1 is \\\\(marks.mark1)")print("Mark2 is \\\\(marks.mark2)")print("Mark3 is \\\\(marks.mark3)")\\nThe output of the above program execution is:\\n\\nMark1 is 100, Mark2 is 78, Mark3 is 98\\nIn the instance, we access the student's grades through the structure name 'studentMarks'. The structure members are initialized as mark1, mark2, mark3, and the data type is integer.\\n\\nThen we instantiate the structure studentMarks() using the let keyword and pass it to marks.\\n\\nFinally, we access the values of the structure members using the . dot notation.\\n\\nThe following instance passes values when instantiating the structure and clones a structure:\\n\\nimport Cocoastruct MarksStruct { var mark: Int init(mark: Int) { self.mark = mark }}var aStruct = MarksStruct(mark: 98)var bStruct = aStruct // aStruct and bStruct is a structure using the same values! bStruct.mark = 97print(aStruct.mark) // 98print(bStruct.mark) // 97\\nThe output of the above program execution is:\\n\\n9897\\n\\n* * *\\n\\n## Structure Application\\n\\nIn your code, you can use structures to define your custom data types.\\n\\nStructure instances are always passed by value to define your custom data types.\\n\\nAs a general guideline, consider building a structure when one or more of the following conditions are met:\\n\\n* The primary purpose of the structure is to encapsulate a small amount of related simple data values.\\n* It is reasonable to expect that the encapsulated data will be copied rather than referenced when a structure instance is assigned or passed.\\n* Any value-type properties stored in the structure will also be copied rather than referenced.\\n* The structure does not need to inherit properties or behaviors from another existing type.\\n\\nFor example, structures are suitable in the following scenarios:\\n\\n* The size of a geometric shape, encapsulating a width property and a height property, both of which are Double types.\\n* A path within a certain range, encapsulating a start property and a length property, both of which are Int types.\\n* A point in a 3D coordinate system, encapsulating x, y, and z properties, all of which are Double types.\\n\\nStructure instances are passed by value rather than by reference.\\n\\nimport Cocoastruct 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 }}print("Excellent grade:")var marks = markStruct(mark1: 98, mark2: 96, mark3:100)print(marks.mark1)print(marks.mark2)print(marks.mark3)print("Poor grade:")var fail = markStruct(mark1: 34, mark2: 42, mark3: 13)print(fail.mark1)print(fail.mark2)print(fail.mark3)\\nThe output of the above program execution is:\\n\\nExcellent grade:9896100Poor grade:344213\\nIn the above instance, we defined the structure markStruct with three member properties: mark1, mark2, and mark3. The self keyword is used within the structure to refer to the member properties.\\n\\nFrom the instance, we can well understand that structure instances are passed by value.