Posts

Showing posts with the label unwrap

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

Optionals

The last few posts have been a summary and consolidation of what I know about Swift. It was a cathartic process, talking through what I know about it at this point and reminding myself of why I am learning it too! This blog will from now on detail my immediate thoughts and reflections about what I learn with Swift. This one starts from page 52 of 'Learn to Code in Swift 4' by Kevin McNeish. The first few chapters have been an introduction to Swift, how to navigate the Ebook, a bit about Xcode and about objects/classes. This feels like a good point to begin my 'live' reflections: Optionals ! Optionals allow you to specify that a variable or constant may return 'nil' (nothing). Now what exactly that means I'm not sure about, at this point. They seem to be an important element of Swift so I need to get my head around them. Apparently they let you be more precise - telling the code when it's OK for a value to be empty, and when not. So, if an unexpected ...