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

Multiple Optional Bindings in Swift

Xcode 11.6 Swift 5.2.4

Last updated on August 12, 2020

Shortly after my last post Segue from UITableViewCell Taps in Swift, Apple released Xcode 6.3 Beta 1, which includes Swift 1.2.  There are many updates to the language that I was quite happy about, but let’s talk about my favorite one today, improved Optional Binding.

The new version allows you to bind multiple optionals, as well as check a boolean expression related to them in a single line, avoiding the nesting we currently must do when working with multiple Optional Bindings.
[Read more…]

Filed Under: Swift Tagged With: optionals, 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

Getting Started With UITableView in Swift

Xcode 11.6 Swift 5.2.4

Last updated on August 12, 2020

One of the most common Views used in iOS apps is the UITableView.  In the default iPhone apps alone, the only ones that I didn’t easily find what is PROBABLY a UITableView was in Newsstand, Videos, Camera, and the Calculator.  Even then, the first two of those appear to use UITableView’s close cousin UICollectionView.  With the exception of the Settings app itself, I wasn’t even counting their settings pages (which probably all of them have, and show up as a UITableView either in their own app or via the Settings app).

You probably want to know how to use such an important view?  There is too much to go into all of it today, but let’s start out with how to get one working and fill it with data.
[Read more…]

Filed Under: Tutorial Tagged With: optionals, 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

  • 1
  • 2
  • 3
  • 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