Posts

Showing posts with the label guard

Stephen DeStefano Swift 5 Course - Part 7 (Lectures 26 to 31)

Image
So continuing with Control Flow! Going to do approximately an hour now - so that will be the rest of this chapter, then the next one on Strings.  Start Time - 13:20 If/else Just a catch up on this - skimming through! Simple example above. The use of else if as the alternative cases. Nested if statements - having an if statement with one that follows immediately. Simple. Switch Statements Again, I'm happy with this. Has to be exhaustive, is better for situations with numbers. Using several values for different cases - And this is with number ranges - Tuples with switch statements - Again, straightforward stuff. You just need to do the case with two values (as there are two - cars and weeks). In the above, there are closed values, the wildcard bit (underscore used). You can also use value binding by creating a value within the case  - So you then use those temp values that have been binned within the print statements. Where clause - can be u...

Bob Lee Course Part 2 (lessons 4 to 5)

Image
Moving on with Bob's course. Like I said, I enjoyed the last part as it did help to explain optionals - even though he was waffly and not always clear. Still, going to crack on with the next one. Start Time - 19:28 Optional Chaining *This time I'm going to create an Xcode playground to code along with the lesson. let labelColor = UILabel (). highlightedTextColor ?. cgColor So this is an example of an optional in action.  *I can just copy this from the course website - save some time! class Human {     var name: String     init (name: String ) {         self . name = name     }          func sayHello() {         print ( "Hello, I'm \ ( name )" )     } } class Apartment {     var human : Human ? = nil   } What we're showing in the Apartment class is that the human value is of the Human class but ...

Introduction to Optionals (Part 2)

The second part of optionals! I get Optional Binding so we'll see what we have next... Downsides to using If Let Optional binding is used with the if let. The example Pasan gives highlights a couple of flaws - what if we don't have the data? Well, we would have to use else and nil. struct Friend {          let name: String     let age: String     let address: String ? } func new(friendDictionary: [ String : String ]) -> Friend ? {          let name = friendDictionary[ "name" ]          if name == nil {                  return nil     }          let address = friendDictionary[ "address" ] } The issue here is we would have to return the values using ! which is not recommended. The main point Pasan makes is that optional binding is laborious but it was necessary...