Swift Structures with Examples
Job-ready Online Courses: Dive into Knowledge. Learn More!
Structures are user-defined data types. It groups similar data into a single unit. It is also known as Structs. It can be used as an alternative for classes in some cases. In this article, we’ll learn about its syntax and usage with relevant examples.
Swift Struct Syntax
To define a struct, we write the struct keyword followed by the name of the structure. We enclose its methods or properties within it using curly braces {}
Struct structName{
property1
property2
...
propertyN
}For example,
struct websiteInfo{
var name: string
var website: string
}Instance of a Struct in Swift
We create an instance to use the structures. After creating the instances, the properties and methods can be accessed.
Syntax of creating an instance of struct.
let/var exampleInstance = exampleStruct()
For example,
struct websiteInfo{
var name: String = "DataFlair"
var website: String = "https://data-flair.training/"
}
var myWebsite = websiteInfo()
print(myWebsite)Output:
websiteInfo(name: “DataFlair”, website: “https://data-flair.training/”)
Memberwise Initializers
Instead of assigning the values to the properties of the struct during declaration, we can assign values while creating the instance of the struct. These are also called memberwise initializers.
Syntax of memberwise initializers:
let exampleInstance = exampleStruct(property1: val1, property2: val2, ..., propertyN:valN)
For example,
struct websiteInfo{
var name: String
var website: String
}
var myWebsite = websiteInfo(name: "DataFlair", website: "https://data-flair.training/" )
print(myWebsite)Output:
websiteInfo(name: “DataFlair”, website: “https://data-flair.training/”)
Accessing a Struct
We can access the properties of the strict using dot (.) notation.
Syntax for accessing strict properties.
let exampleInstance = exampleStruct(property1: val1) var example = exampleInstance.property1
For example,
struct websiteInfo{
var name: String = "DataFlair"
var website: String = "https://data-flair.training/"
}
var myWebsite = websiteInfo()
print("Name of the website: \(myWebsite.name)")
print("Link of the website: \(myWebsite.website)")Output:
Name of the website: DataFlair
Link of the website: https://data-flair.training/
Function Inside a struct
A structure can also include a method/function within it. It might use the properties in its function. To access a function, we use the dot notation as we use to access the struct properties.
Syntax of a function inside a struct.
Struct structName{
property1
property2
Func functionName(){
// statements
}
}For example,
struct websiteInfo{
var name: String
var website: String
func helloFunction(name: String){
print("Hello from \(name)!")
}
}
var myWebsite = websiteInfo(name: "DataFlair", website: "https://data-flair.training/" )
myWebsite.helloFunction(name: myWebsite.name)Output:
Hello from DataFlair!
Swift Structures: A Value Type
Structures are value types of data structures. This means that the values are copied while assigning them to instances, variables, constants or while passing through a function. Every time the value is passed around in the code, the values are copied. This is also known as the data is passed by value rather than reference.
Conclusion
Structures are user-defined custom data types. It helps us manage complex data in a simpler format. This article covers the definition and usage of swift structures. It shows how we can access its methods and properties. This also helps us understand its appropriate usage with the help of relevant examples.
Did you like this article? If Yes, please give DataFlair 5 Stars on Google

