Ray Wenderlich Functions and Types Course - Part 7
Here we go! Tricky to get much done this week as I've been working on an application for a potential promotion at work. This is a big deal - if I do get it, it will mean a substantial difference to me short and long term. If I don't, then it may well be a sign that I need to ease my way away from teaching... we shall see! In the mean-time, back to Catie's course. Still a fair bit to do. Aiming to finish it in a few more entries.
Start Time - 17:48
Switch Statements
All of this is fine so far. Enums from before used...
Start Time - 17:48
Switch Statements
All of this is fine so far. Enums from before used...
enum Month: Int {
case january = 1, february, march, april, may, june, july, august, september, october, november, december
}
enum Season: String, CaseIterable {
/// ☃️
case winter
/// 🌸
case spring
/// 😎
case summer
/// 🍂
case autumn
}
func getSeason(month: Month) -> Season {
switch month {
case .november, .december, .january, .february:
return .winter
case .september, .october:
return .autumn
case .march, .april:
return .spring
case .may, .june, .july, .august:
return .summer
}
}
getSeason(month: .april)
For switch with enum it makes total sense to have the options on one line - for the different months.
Some cool stuff below -
func getDescription(for number: Int) -> String {
switch number {
case 0:
return "Zero"
case 1...9:
return "Between 1 and 9"
case let negativeNumber where negativeNumber < 0:
return "Negative number"
case _ where number > .max / 2:
return "Massive number!"
default:
return "No description"
}
}
Value binding - using the let and the where. It binds the amount to a specific value.
let number = Int.max
let numberIsEven: Bool
switch number % 2 {
case 0:
numberIsEven = true
default:
numberIsEven = false
}
The above is doing it outside a function.
Cases are evaluated in order
func pointCategory( for coordinates: (Double, Double)) -> String {
switch coordinates {
case (0, 0):
return "Origin"
case (let x, 0):
return "On the x axis at \(x)"
case (let y, 0):
return "On the y axis at \(y)"
case let (x, y):
return "You are at point \(x), \(y)"
default:
return "No coordinate plots"
}
}
So you need to ensure that it is logical in order. Also, we don't need the default here!
Here is the next version -
func pointCategory( for coordinates: (Double, Double)) -> String {
switch coordinates {
case (0, 0):
return "Origin"
case (let x, 0):
return "On the x axis at \(x)"
case (0, let y):
return "On the y axis at \(y)"
case _ where coordinates.0 == coordinates.1:
return "Along x = y"
case let (x, y) where y == x * x:
return "Along y = x squared"
case let (x, y):
return "You are at point \(x), \(y)"
}
}
Challenges!
func ageCategoriser(name: String, age: Int) -> String {
switch age {
case 0...2:
return "\(name) is an infant"
case 3...12:
return "\(name) is a child"
case 13...19:
return "\(name) is a teenager"
case 20...39:
return "\(name) is an adult"
case 40...60:
return "\(name) is middle aged"
case 61...:
return "\(name) is elderly"
default:
return "\(age) not recognised"
}
}
So the first one all good! Only that I did default for elderly....I can actually do an indefinite ... then do the default for other erroneous values...
Next one harder but actually had MOST of it without help!
enum Direction {
case north, south, east, west
}
func getDirection(movements: [Direction]) -> (x: Int, y: Int) {
var location = (x: 0, y: 0)
for movement in movements {
switch movement {
case .north:
location.y += 1
case .south:
location.y -= 1
case .east:
location.x += 1
case .west:
location.x -= 1
}
}
return location
}
Alternative - with closure!
Paused at 18:39; restart at 18:44
Associated Values
Using label names - like you do with function parameters!
Here is an example....
enum TwoDimensionalPoint {
case origin
case onYAxis(Double)
case onXAxis(Double)
case noZeroCoordinate(x: Double, y: Double)
}
let coordinates = (1.0, 3.0)
let twoDimensionalPoint: TwoDimensionalPoint
switch coordinates {
case (0, 0):
twoDimensionalPoint = .origin
case (_, 0):
twoDimensionalPoint = .onXAxis(coordinates.0)
case (0, _):
twoDimensionalPoint = .onYAxis(coordinates.1)
default:
twoDimensionalPoint = .noZeroCoordinate(x: coordinates.0, y: coordinates.1)
}
So the type is put in brackets for the enum. There are label names for the tuple too.
So I've rushed through the next example a bit - quite oddly with all of the options so may need to go over that.
Finish Time - 18:56 (total 1 hour 3 minutes)
Great session! Need to go now so onto the next chapter next time!
Comments
Post a Comment