Structures and Classes
- The Coach
- May 10, 2020
- 3 min read
Ever since I started this coding journey I have been perplexed about whether my code should always include both classes and structures. On further reading of the Swift Guide 5.1 its becoming apparent that I can for most of the part just focus on using structures for the majority of casesI will attempt to explain this as we progress through their characteristics, through the use of some examples.
Before we start thinking about what makes them different, lets first understand what the commonality is between them, which is the probable reason why beginners like me get confused on when to use them.
Common aspects :
They both define properties to store values and are able to support the inclusion of methods to enable functionality in the code.
You can access their values throughout your programme by using the subscript syntax.
They both can have their initialisers defined to setup their initial state.
You can extend them through extensions to introduce new / expand their functionality beyond the initial implementation / creation of the class/structure.
All structures are value types in Swift. This means that any structure and instances you create—and any value types they have as properties—are always copied when they are passed around in your code.... (swift guide 5.1)
Illustrative Concept of Structures and Classes
Use case : A bank makes a payment to another bank.
The bank has to have list of various banks around the world. For simplicity I am using a structure.
The bank has to send a payment to the other bank and this is in the shape of a class
We create a structure called Beneficiary Bank as all remittances need to be sent to a beneficiary bank, and as such we need to know the details of the bank which will be quite standard.
struct BeneficiaryBank {
var bankSwiftCode = 0
var bankName: String = ""
var bankAddress: String = ""
)
Lets create a class called Remittance as this is going to be the actual remittance information which needs to be submitted and that comprises of beneficiary bank information and remitter information
class Remittance {
var beneficiarybank = BeneficiaryBank()
var beneficiaryAccountNumber = 0
var remitterName: String = ""
var beneficiaryAccountName: String = ""
var amount = 0
}
Now lets create an instance of the structure and class.
let someBeneficiaryBank = BeneficiaryBank() //linked to a Structure -> Beneficiary Bank
let someRemittance = Remittance() // linked to a Class -> Remittance
TIP : Now we can use the dot syntax to create a new value to the variable property
someRemittance.beneficiarybank.bankSwiftCode = 111111
TIP: You can now access the subproperties of the Remittance Class
print("The beneficiay bank SWIFT Code is \(someRemittance.beneficiarybank.bankSwiftCode)")
You can now access the subproperties of the Structure
print(someBeneficiaryBank.bankSwiftCode)
See the structure at play
So now we can set a constant of bankBigBank and initialise it. Since BeneficiaryBank is a structure we can automatically generate a memberwise initialiser - which we use to initiaise the memeber proprtise of new structure instance.
let bankBigBankHeadOffice = BeneficiaryBank(
bankSwiftCode: 22222000,
bankName: "Big Bank Ltd",
bankAddress: "Big Bank Street")
let bankSmallBankHeadOffice = BeneficiaryBank(
bankSwiftCode: 333333,
bankName: "Small Bank Ltd",
bankAddress: "Small Bank Street")
print("The Swift Account Number for \(bankBigBankHeadOffice.bankName) is \(bankBigBankHeadOffice.bankSwiftCode)")
So we can then declare a variable to link all the beneficiary bank branches of the bankBigBank through the variable
var bigBankBranch1 = bankBigBankHeadOffice
var bigBankBranch2 = bankBigBankHeadOffice
We can now amend the existing property for each of the branches
bigBankBranch1.bankSwiftCode = 22222001
bigBankBranch1.bankName = "Big Bank Ltd - Branch One"
bigBankBranch2.bankSwiftCode = 22222002
print(BeneficiaryBank.self, bigBankBranch1)
Bit more on Classes
Classes are reference types and are not copied when they are assigned to a variable of a constant, or when they are passed into a function. Rather than a copy a reference to the same existing instance is used.
let payment1 = Remittance()
payment1.beneficiaryAccountName = "JoJo"
payment1.beneficiaryAccountNumber = 676767
payment1.beneficiarybank = bigBankBranch1
payment1.remitterName = "Roger Rabbit"
payment1.amount = 7000
let payment2 = Remittance()
payment2.beneficiaryAccountName = "JoJo"
payment2.beneficiaryAccountNumber = 676767
payment2.beneficiarybank = bigBankBranch1
payment2.remitterName = "Roger Rabbit"
payment2.amount = 6000
So for each payment that made we leverage the class to fallout the full remittance information. There is a lot more nitty gritty on Classes which needs to be covered, look out for my next post!
Comments