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

Enumerations in Swift

Xcode 11.6 Swift 5.2.4

Last updated on August 10, 2020

I have mentioned enumerations in three previous posts (Computed Properties in Swift, Swift Optionals – Declaration, Unwrapping, and Binding, and Loops, Switch Statements, and Ranges in Swift).  It is probably about time to actually talk about them, eh?  In C, and even Objective-C, enumerations were little more than glorified aliases for integer values.  In Swift though, enumerations have been given significantly more power.  In Swift, enumerations are a lot more like classes or structs, on top of the actual enumeration values themselves.  For the moment though, we will talk about what makes an enumeration an enumeration.
[Read more…]

Filed Under: Swift

Loops, Switch Statements, and Ranges in Swift

Xcode 11.6 Swift 5.2.4

Last updated on August 10, 2020

I’m going to go a bit old school on this one.  We’re staying on Swift here, don’t worry, but I want to cover something that has been around since the early days of C.  They were probably earlier, but that’s the oldest language besides BASIC that I personally have experience with.  We’re going to talk about some classic control flow and how Swift has updated them… or kept them the same, we’ll see.
[Read more…]

Filed Under: Swift

Functions in Swift: Parameters and Return Types

Xcode 11.6 Swift 5.2.4

Last updated on August 10, 2020

I think we’ve gone over the basics of variables in Swift enough.  It is time to talk about functions in Swift.  Now, we have talked about these to some extent before, and I took it as a given that people would understand a bit.  But now I think it is time to actually start diving in and explaining more specifics.

Functions are blocks of code that perform a task.  You can then call these functions at different places, which makes fixing bugs much easier than the alternative.  If you just copied and pasted the code everywhere, and you found a bug, you would have to change each one to fix that bug.  With functions, you just change it one place, and everything that calls it gets the new fixed version of that block of code.  The entire app itself is wrapped within a function called “main”, but you don’t normally interact with it, unless there’s a crash.  Functions usually take arguments, and very often return data back to where it was called from.
[Read more…]

Filed Under: Swift

Access Control in Swift

Xcode 11.6 Swift 5.2.4

Last updated on August 10, 2020

The access control in Swift is much more like the access control I am accustomed to in other languages like C# or Java, and I am quite thankful for that.  I’m not saying there is anything wrong with Objective-C’s way of doing it, while it was different, I did like having the private properties in the interface block of the implementation (.m) file, and the public properties and methods in the header (.h) file.  It made it easy to know exactly where to look.

Nonetheless, access control is here in Swift, so here we go.  You can set access control to many things in Swift, particularly properties and methods, but you can even set it to types, initializers, subscripts, or protocols.  Swift currently has three levels of access control:

  • Public
  • Internal
  • Fileprivate
  • Private

[Read more…]

Filed Under: Swift Tagged With: Access Controls

  • « Previous Page
  • 1
  • …
  • 6
  • 7
  • 8
  • 9
  • 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