Classes
ref – https://code.tutsplus.com/tutorials/swift-from-scratch-an-introduction-to-classes-and-structures–cms-23197
Create a basic class:
1 2 3 |
class Person { } |
First, we must create instance variables (ivars). The ivars are to be private because we do not want others to access.
Also, we use optional so it can have nil values.
We declare private as the accessor. Then use var, so that the ivar’s state can be changed.
1 2 3 4 5 6 7 |
class Person { // Implicit in the declaration of an optional with no value set, // is the assignment to nil private var firstName: String? //optional variables, mutable private var lastName: String? //optional variables, mutable } |
Initializers
Initializers are called to create a new instance of a particular type. In its simplest form, an initializer is like an instance method with no parameters, written using the init keyword.
#function is a macro used to display the local scope’s function call name.
print is a function used to log strings to the console.
self is to reference the current instance working instance.
You can have overloaded functions, with different parameters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// default initializer init() { print("--> \(#function)") self.firstName = "NA"; self.lastName = "NA"; self.gender = nil; print("<-- \(#function)") } // name initializer init(first: String, last: String) { print("--> \(#function)") self.firstName = first; self.lastName = last; self.gender = nil; print("<-- \(#function)") } // name initializer init(first: String, last: String, gender: String) { print("--> \(#function)") self.firstName = first; self.lastName = last; self.gender = gender; print("<-- \(#function)") } |
Functions
This function is called toStringFromArray
Accessor is private
parameter name is data
parameter label is OfStrings
parameter is an array of type String as denoted by [String]
return value is String as denoted by -> String
We have a variable called total that is of type String. It is initialized to empty string literal.
We then use for in to loop through the array “data”. The temp variable “aString” used in the loop
it to denote the string value at each index. We add that string to our total string.
We basically append all the strings from the array data, into the string total. Then return it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
private func toStringFromArray(OfStrings data: [String] ) -> String { print("--> \(#function)") var total: String = "" for aString in data { total += aString + " "; } print("<-- \(#function)"); return total; } |
In order to use this function we go
1 2 3 4 5 |
// declare an array of type String. var parts: [String] = [] // call it like this, and throw in the parts array like the interface asks self.toStringFromArray(OfStrings: parts); |
We will be using toStringFromArray in another function like so.
function name is fullName
return type is String
accessor is private
It basically does optional binding to get the values of our instance variables.
then it calls our private function toStringFromArray in order to take an array, and
return a String variable with all the array’s string data int it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
// returns a String private func fullName() -> String { print("--> \(#function)") // array named parts // array of type String var parts: [String] = [] // optional binding..is it set? if let firstName = self.firstName { // if its set, let's add it to the array parts += [firstName] } if let lastName = self.lastName { // if its set, let's add it to the array parts += [lastName] } print("<-- \(#function)") return self.toStringFromArray(OfStrings: parts); } |
Unwrapping Conditional
Now that we have the data we need from fullName function, and a instance variable,
we can unwrap conditional for the instance variable self.gender. Then place it together with fullName
to return the Person’s full information
1 2 3 4 5 6 7 8 9 10 |
public func fullInfo() -> String { // let variable g take on value. if let g = self.gender { return "\(self.fullName()), \(g)"; } return "Sorry, data is incomplete"; } |
Underscore _
underscores are used as the parameter label to denote that the label is optional
1 2 3 4 5 |
public func test2(_ data: String, _ data2: String) { print("--> \(#function)") // calculate here print("<-- \(#function)") } |
You can then call the function without denoting the parameter labels
1 |
person2.test2("pqiweurpqiwuerp", "hohho") |
Using just the parameter names, no label
1 2 3 4 5 |
public func test(AString: String) { print("--> \(#function)") print("<-- \(#function)") } |
You can also use just use the parameter name like so:
1 |
person2.test(AString: "hahahahah") |
Full Source Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
// // main.swift // MigrationPractices // // Created by Ricky Tsao on 2/3/17. // Copyright © 2017 Epam. All rights reserved. // import Foundation print("Hello, World!") class Person { // Implicit in the declaration of an optional with no value set, // is the assignment to nil private var firstName: String? //optional variables, mutable private var lastName: String? //optional varaibles, mutable let gender: String? //immutable, can only be initialized ONCE //Initializers are called to create a new instance of a particular type. In its simplest form, an initializer is like an instance method with no parameters, written using the init keyword: // default initializer init() { print("--> \(#function)") self.firstName = "NA"; self.lastName = "NA"; self.gender = nil; print("<-- \(#function)") } // name initializer init(first: String, last: String) { print("--> \(#function)") self.firstName = first; self.lastName = last; self.gender = nil; print("<-- \(#function)") } // name initializer init(first: String, last: String, gender: String) { print("--> \(#function)") self.firstName = first; self.lastName = last; self.gender = gender; print("<-- \(#function)") } public func fullInfo() -> String { // let variable g take on value. if let g = self.gender { return "\(self.fullName()), \(g)"; } return "Sorry, data is incomplete"; } // returns a String private func fullName() -> String { print("--> \(#function)") // array named parts // array of type String var parts: [String] = [] // optional binding..is it set? if let firstName = self.firstName { // if its set, let's add it to the array parts += [firstName] } if let lastName = self.lastName { // if its set, let's add it to the array parts += [lastName] } print("<-- \(#function)") return self.toStringFromArray(OfStrings: parts); } // takes in parameter of an "Array of type String" // this parameter's reference is called "data" // parameter name is called "OfStrings" // func returns a String ////In Swift, functions have both parameter labels and parameter names // "parameter label" would be "OfStrings" // "parameter name" would be "data" private func toStringFromArray(OfStrings data: [String] ) -> String { print("--> \(#function)") var total: String = "" for aString in data { total += aString + " "; } print("<-- \(#function)"); return total; } // using underscore as the parameter label simply means the label is optional // and that you don't have to do any parameter labeling. // just do this: person2.test2("pqiweurpqiwuerp") public func test2(_ data: String, _ data2: String) { print("--> \(#function)") print("<-- \(#function)") } // you can also use just use the parameter name like so: // person2.test(AString: "hahahahah") public func test(AString: String) { print("--> \(#function)") print("<-- \(#function)") } //explicit - state clearly and in detail //implicity - imply...suggest. } var person1 = Person(first: "Ricky", last: "Tsao", gender: "Male") print("The full name is: \(person1.fullInfo())") var person2 = Person(first: "Lily", last: "Gan") print("The full name is: \(person2.fullInfo())") person2.test(AString: "hahahahah") person2.test2("pqiweurpqiwuerp", "hohho") |
Output:
Hello, World!
–> init(first:last:gender:)
<-- init(first:last:gender:)
--> fullName()
<-- fullName()
--> toStringFromArray(OfStrings:)
<-- toStringFromArray(OfStrings:)
The full name is: Ricky Tsao , Male
--> init(first:last:)
<-- init(first:last:)
The full name is: Sorry, data is incomplete
Program ended with exit code: 0