Posts

Showing posts with the label nil

Ray Wenderlich Course Part 9 (up to lesson 79)

Image
So back to the situation of not having much time to write! Am starting this entry now and will continue it on the train. Once I've got functionals and optionals done then that will do for now. Again, I'm going at x 1.5 speed as it's mostly recap. It will be the challenges to really test my mettle! I'm also not going to type examples along - again, it's mostly recap! Functions So just to sum up, you can have no parameters and have code within the curly braces e.g. something to print, or you can have values within the parameters. Within the parameters, you can specify the values or just name the parameter and the type. You can use the _ for an unnamed parameter. The return option -> means you use the return keyword rather than a print function. A little bit in the Q&A from Ray about the distinction between functions and methods: In the old days of programming before Object-Oriented Programming was invented, everyone used to only use functions (usin...

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...