Swift Structures

Structure is an flexible structure to define properties and function in our source code. That are used in a to create building block structure of programming. We can define structure as follows.

struct StructureName {
    // structure definition
}

struct is an structure define keyword, after that the provide name of structure. Body of structure is surrounded by curly braces {}. And within that we can define properties and functionality of structure. When we are create a structure that means we are create new data type in swift programming. In naming conversion Data type should be capitalized in form of UpperCamelCase. In this case StructureName is an identifier to structure. let's define properties of structure.

struct Book{
    
    var serialNo : Int
    var authorName : String? //optional field (possible to set nil)
    var bookName : String
    var bookPrice : Double
}
//providing the properties value of struct Book
let swiftBook = (serialNo:12,
                 bookName:"Swift Fundamental",
                bookPrice:1000.30,
                authorName:"XYZ")

//access properties
print("Name: \(swiftBook.bookName)")
print("Author: \(swiftBook.authorName)")
print("Serial No: \(swiftBook.serialNo)")
print("Price: \(swiftBook.bookPrice)")

Output

Name: Swift Fundamental
Author: XYZ
Serial No: 12
Price: 1000.3

Define and Accessing structure Element

//Make a coordinate structures
struct Coordinates{
    //Define propties
    var xAxis=0
    var yAxis=0
}

We can access the structure element of using instance. That is similar to access element of class member.

//Make a coordinate structures
struct Coordinates{
    //Define propties
    var xAxis=0
    var yAxis=0
}

//Make instance of Coordinate structure
let obj=Coordinates()

//Use element value
print("X-Axis : \(obj.xAxis)")
print("Y-Axis : \(obj.yAxis)")

Output

X-Axis : 0
Y-Axis : 0

dot operator are used to access particular properties of structure. We can be modified the structure element by dot and specified member element.

//Make a Circle structures
struct Circle{
    //Define propties
    var xAxis=0
    var yAxis=0
    var radious=0
}

//Make instance of Circle structure
var obj=Circle()

//update element
obj.xAxis=100
obj.yAxis=100
obj.radious=80

//access element value
print("X-Axis : \(obj.xAxis)")
print("Y-Axis : \(obj.yAxis)")
print("radious : \(obj.radious)")

Output

X-Axis : 100
Y-Axis : 100
radious : 80

Similar way we can modified structure element value by using instance. Memberwise initializer are possible to structure instance, that is useful when multiple instance are create in same code.

//Make a Circle structures
struct Circle{
    //Define propties
    var xAxis=0
    var yAxis=0
    var radious=0
}

//Make instance of Circle structure
var firstCircle=Circle(xAxis:100,yAxis:100,radious:80) //for firstCircle
var secondCircle=Circle(xAxis:70,yAxis:100,radious:50) //for secondCircle

//Display firstCircle element
print("First Circle")
print("X-Axis : \(firstCircle.xAxis)")
print("Y-Axis : \(firstCircle.yAxis)")
print("radious : \(firstCircle.radious)")

//Display secondCircle element
print("Second Circle")
print("X-Axis : \(secondCircle.xAxis)")
print("Y-Axis : \(secondCircle.yAxis)")
print("radious : \(secondCircle.radious)")

Output

First Circle
X-Axis : 100
Y-Axis : 100
radious : 80
Second Circle
X-Axis : 70
Y-Axis : 100
radious : 50

Accessing reference of struct member

struct Team{
    
    var name : String
    var group : String

}

struct GroupMatch{

    let a : Team
    let b : Team

}

struct MatchSchedule{
    
    let teams  : GroupMatch
    let date : String 
    let time  : String 
    let machType : String
}

let indianTeam = Team(name:"IND",group:"A")
let australiaTeam = Team(name:"AUS",group:"B")


let teamInfo = GroupMatch(a:indianTeam,b:australiaTeam)


let schedule = MatchSchedule(teams:teamInfo,date:"20-12-2020",time:"7:PM",machType:"20-20")

print("Match : \(schedule.teams.a.name) VS \(schedule.teams.b.name)")
print("Group Match : \(schedule.teams.a.group) And \(schedule.teams.b.group) Final")
print("Date : \(schedule.date) (\(schedule.time))")

Output

Match : IND VS AUS
Group Match : A And B Final
Date : 20-12-2020 (7:PM)

Define method within structure

We can define method inside of structure that are same as define method of class. And access this method by instance of structure.

struct Version {
    var value = 0
    mutating func increment(){
        value += 1
    }
    func get() -> Int{
        return value
    }
}
var version = Version()

version.increment() //execute function
version.increment()

print(version.get()) //2

Output

2

mutating are used to modify particular instance field value.



Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved