Ray Wenderlich Functions and Types Course - Part 9
Five days between entries is not ideal! But I'm going to crack on. Will try to get 2-3 sessions in during the current week. Let's go!
Start Time - 19:50
Installing additional components on Xcode as the mac system has been updated just now...
CaseIterable - a protocol used so that you can then access the cases with more functionality.
There is a lot going on here!
So after the CaseIterable bit, we have a method (advance) within the enum. It is using Unsigned Int (UInt). That's supposed to be better as no negative numbers will be involved so this is apparently the better one to use.
So even more technical....
Start Time - 19:50
Installing additional components on Xcode as the mac system has been updated just now...
CaseIterable - a protocol used so that you can then access the cases with more functionality.
There is a lot going on here!
enum Weekday: CaseIterable {
case monday, tuesday, wednesday, thursday, friday, saturday, sunday
func advance(by dayCount: UInt) {
let indexOfToday = Weekday.allCases.firstIndex(of: self)!
}
}
Weekday.allCases
var weekday: Weekday = .sunday
So after the CaseIterable bit, we have a method (advance) within the enum. It is using Unsigned Int (UInt). That's supposed to be better as no negative numbers will be involved so this is apparently the better one to use.
So even more technical....
enum Weekday: CaseIterable {
case monday, tuesday, wednesday, thursday, friday, saturday, sunday
mutating func advance(by dayCount: UInt) {
let indexOfToday = Weekday.allCases.firstIndex(of: self)!
let indexOfAdvancedDay = indexOfToday + Int(dayCount)
self = Weekday.allCases[indexOfAdvancedDay % Weekday.allCases.count]
}
}
A hell of a lot going on. The constants created are to essentially get an index of days in order, then add on a number of days - the dayCount. Eventually, the self becomes depending on what day we are on, added to the number of days forwarded from there.
And there is this..
struct Time {
var day: Weekday
var hour: UInt
init(day: Weekday, hour: UInt = 0) {
self.day = day
self.hour = hour
}
mutating func advance(byHours hourCount: UInt) {
let (dayCount, hour) = (self.hour + hourCount).quotientAndRemainder(dividingBy: 24)
day.advance(by: dayCount)
self.hour = hour
}
}
I get using mutating again - this is because the value of weekDay could change with the advance function. We have a quotientAndRemainder method used, which gets a dayCount and hour value (a tuple). It's divided by 24 for 24 hours in a day, so it can go forward a day.
Mutating means more that you are going to make a whole new struct or enum.
enum Mathematics {
static func getLength(x: Double, y: Double) -> Double {
(x * x + y * y).squareRoot()
}
}
The above is a caseless enumeration. It is great for organisation.
The Apple Library has so many different data types - the advice is not to create your own as there are so many robust types out there already!
Challenge!
This was really, really hard. It's clear that this is now way out of my comfort zone. I'm going to persevere though as I have found this course generally useful.
Computed Properties vs Methods
So when to use which? Properties hold values and methods perform work. Some of this is an issue of style. Best thing to do is experiment, then develop own guidelines to decide which is best!
So to put this in perspective, the properties part actually went well. The methods bit was much harder.
Next time it will be protocols! That will be the last chapter - it will need a couple of entries.
Finish Time - 20:33 (43 minutes)
There are quite a few courses popping up on RW.com. I will have a look soon to decide what to focus on next. It's good to know I am paid up for the year and have total access. Having said that, I do need to do a consolidation entry, looking over each aspect of this course again to make it clearer what I've learned. This Half Term will be a better chance to really get into Swift more and get some things clearer in my head, with a practical project to try out too.
Comments
Post a Comment