Coding Explorer Blog

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

  • Home
  • Apps
  • About
  • Contact

The Guard Statement in Swift

Xcode 11.6 Swift 5.2.4

Last updated on August 12, 2020

Often when working with Swift Optionals, we will want to perform an action if the Optional contains a value, or do something else if it does not.  This is done with Optional Binding with the “if let” syntax, which lets us test for wether it contains a value, and if it does, binds it to a constant that can be used later in the if statement.  This works great for many cases, but it does put the emphasis on the positive case.  If there is a value, do this, otherwise do that.  Now if the code to run when there is a value is short, you can easily see what the “else” clause is tied to.  However, if the code to run when there is a value is long, then you might need to do some scrolling to see the associated else clause.

This is where the guard statement comes in handy, introduced with Swift 2.  The guard statement puts the emphasis on the error condition.  In this case, if there is no value, you can clearly see what will be done to deal with it.

What happens if there is a value?  The rest of the current scope continues running like normal.  Let’s go into an example of how the guard statement can be helpful.
[Read more…]

Filed Under: Swift Tagged With: optionals, properties, Swift

Segue from UITableViewCell Taps in Swift

Xcode 11.6 Swift 5.2.4

Last updated on August 12, 2020

In many apps that use a UITableView, when a UITableViewCell is tapped, the screen will segue over to another view, such as how choosing a song in the Music app goes to the Now Playing screen, and plays that song, shows the album art, and other metadata about the song.  This can be done programmatically in tableView:didSelectRowAtIndexPath:, but with Storyboards, it is even easier.  All we have to do is set up a segue, and treat it pretty much exactly like we did in Segue between Swift View Controllers.

To avoid an even longer setup section, and to not cover things we already covered, we’re going to start with the result of the previous post Getting Started With UITableView in Swift.

Anyway, starting from where we left off, let’s set up the storyboard.
[Read more…]

Filed Under: Tutorial Tagged With: optionals, properties, Swift

Segue between Swift View Controllers

Xcode 11.6 Swift 5.2.4

Last updated on August 12, 2020

So, last time we learned how to make a simple Hello World app on a single view controller.  As fancy as it was, you probably want more than one screen in your app, right?

Perhaps you want a way to segue into the next screen?

That’s what we’re going to go over today, as well as one of the simple ways to share data between view controllers.
[Read more…]

Filed Under: Tutorial Tagged With: Access Controls, optionals, properties, Swift

Designated Initializers and Convenience Initializers in Swift

Xcode 11.6 Swift 5.2.4

Last updated on August 12, 2020

Today we will be learning about another aspect of Class Initializers.  The Swift language has two different types of initializers they are called designated initializers and convenience initializers.  These existed in Objective-C, but a few rules have changed in Swift, and a very helpful keyword was introduced.  We will discuss how designated and convenience initializers are used, and how they can work together to get your classes ready for use.

Designated Initializers

Like most constructs in Swift, designated initializers are aptly named and do exactly what they say they do.  They are the main initializers to be used for a class.  A class must have one designated initializer, but it is not limited to one.  It can have multiple if necessary, but most classes only have one.
[Read more…]

Filed Under: Swift Tagged With: properties

Swift Property Observers

Xcode 11.6 Swift 5.2.4

Last updated on August 12, 2020

In my previous article Class Initializers, I had mentioned property observers, and glossed over them a bit.  Now we’ll discuss them in a bit more depth.

Why use a Property Observer?

Back in Objective-C, if you wanted to do any special handling for setting a property, you would have to override the setter, reimplement the actual value setting (that was originally done for you), and then add whatever you wanted to do besides that, like posting a notification of a change.  Swift’s property observers save you from having to reimplement the setter in those cases.

Property Observers are somewhat similar to computed properties.  You can read more about those in my previous article Computed Properties in Swift.  For computed properties, you write custom code for the getter and setter.  For property observers, you write custom code only for setting, for right before (willSet) and right after (didSet).  The main purpose of Swift’s property observers is to watch for when a property is set.  As such, property observers are only useful for variables (var properties), and cannot be written for constants (let properties).
[Read more…]

Filed Under: Swift Tagged With: properties, Swift

  • 1
  • 2
  • Next Page »

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