Coding Explorer Blog

Exploring how to code for iOS in Swift and Objective-C

  • Home
  • Apps
  • About
  • Contact

Nil Coalescing in Swift

Xcode 11.6 Swift 5.2.4

Last updated on August 10, 2020

Xcode Beta 5 was released with several good changes.  I don’t want this to just be a “What’s new in Xcode Beta 5” post, because once Beta 6 is released, much less the real language, the only point for a post like that is history.  So I am mostly going to talk about my favorite addition to Xcode 6 about optionals, with a short mention of a change that affects it.  This great operator is still available in Swift 2.2 (Xcode 7.3) as well!

Nil Coalescing Operator

I did not see this one coming, but I do like it.  It basically is a way to easily return an unwrapped optional, or a default value.  As with many other parts of optionals, this operator is composed of question marks.  Below is a simple example:

var someOptional: Int? = nil
var aDefaultValue = 42

var theAnswer = someOptional ?? aDefaultValue

Since someOptional is nil (we didn’t set it to a valid Int yet), theAnswer will of course be 42.
[Read more…]

Filed Under: Swift Tagged With: optionals

Optional Chaining and Implicitly Unwrapped Optionals in Swift

Xcode 11.6 Swift 5.2.4

Last updated on August 10, 2020

As promised last time, here is part two of my post about Swift optionals.  Here we will discuss a few more advanced aspects on how to deal with optionals, optional chaining and implicitly unwrapped optionals.  These are a few more tricks to get values out of optionals, while keeping Swift code much cleaner than its predecessors.  Optional chaining makes it easy to drill into multiple levels of optional values.  Implicitly unwrapped optionals, while not as safe as normal optionals, are often used under the assumption that the value that it is describing is never, or usually not, nil.

[Read more…]

Filed Under: Swift Tagged With: optionals

Swift Optionals – Declaration, Unwrapping, and Binding

Xcode 11.6 Swift 5.2.4

Last updated on August 10, 2020

Continuing our conversations about variables in Swift, lets move on to one of the more important additions in this realm to Swift, Optionals.  In Swift, all normal variables must have a value.  In Objective-C, C-based variables (primitives) always had to have a value, but object types could either be a valid object, or a pointer to nil.  Swift optionals allow you to make any type in Swift into a nil-able type (though this nil is not exactly the same as an Objective-C nil).
[Read more…]

Filed Under: Swift Tagged With: optionals

  • « Previous Page
  • 1
  • 2
  • 3

Subscribe to the Coding Explorer Newsletter

* indicates required

Follow Us

Facebooktwitterrss

Recent Posts

  • Error Handling in Swift
  • Creating and Modifying a URL in your Swift App
  • Watch Connectivity in Swift — Application Context
  • watchOS Hello World App in Swift
  • API Availability Checking in Swift

Categories

  • Class Reference
  • General
  • Interface Builder
  • My Apps
  • Objective-C
  • Swift
  • Syntax
  • Tutorial
  • Uncategorized

Archives

  • May 2016
  • March 2016
  • February 2016
  • December 2015
  • July 2015
  • June 2015
  • April 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • January 2014
  • November 2013
  • September 2013
  • August 2013
  • July 2013
  • Terms Of Use
  • Privacy Policy
  • Affiliate Disclaimer

Subscribe to the Coding Explorer Newsletter

* indicates required

Copyright © 2025 Wayne Media LLC · Powered by Genesis Framework · WordPress