This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func dayType(for day: String) -> String { | |
switch day { | |
case: "Saturday", "Sunday": return "Weekend" | |
case: "Monday", "Tuesday", "Wednessday", "Thursday", "Friday": return "Weekday" | |
default: return "This is not a valid date" | |
} | |
} | |
let result1 = dayType(for: "Sunday") //will return "Weekend" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum Day { | |
case Sunday | |
case Monday | |
case Tuesday | |
case Wednessday | |
case Thursday | |
case Friday | |
case Saturday | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Rewriting the date type function */ | |
func dayType(for day: Day) { | |
switch day { | |
case .Saturday, .Sunday: | |
return "Weekend" | |
case .Monday, .Tuesday, .Wednessday, .Thursday, .Friday | |
return "Weekday" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// You can also map to strings | |
enum Week: String { | |
case Sunday = "Weekday" | |
case Monday = "Weekday" | |
case Tuesday = "Weekday" | |
case Wednessday = "Weekday" | |
case Thursday = "Weekday" | |
case Friday = "Weekend" | |
case Saturday = "Weekend" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** associate without labels */ | |
enum Trade { | |
case Buy(String, amount) | |
case Sell(String, Int) | |
} | |
Trade.Buy("Firstbank PLC", 300) | |
Trade.Sell("Firstbank PLC", 700) | |
/** associate with labels */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum AppleDevice { | |
case iPad | |
case iPhone | |
case AppleTv | |
case AppleWatch | |
func description() -> String { | |
return "This is an apple device" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum AppleDevice { | |
case iPad, iPhone, AppleTv, AppleWatch | |
func description() -> String { | |
switch self { | |
case .iPad: return "\(self) was introduced 2006" | |
case .iPhone: return "\(self) was introduced 2007" | |
case .AppleTv: return "\(self) was introduced 2010" | |
case .AppleWatch: return "\(self) was introduced 2014" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Street { | |
var streetName: String? | |
} | |
class House { | |
var noOfRooms = 1 | |
var street: Street? | |
} | |
class Person { | |
var house: House? | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if let myHouse = Person.house, let myStreet = myHouse.street { | |
print(myStreet) //this will access the street | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//if the Person.house fails, it wil not get to the street | |
//which can either return a null or the street | |
let myStreet = Person.house?.street | |
//to make sure we get a value, we can include the if let like below | |
if let myStreet = Person.house?.street { | |
print(myStreet) | |
} |