Coding Explorer Blog

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

  • Home
  • Apps
  • About
  • Contact

Protocol Extensions in Swift

Xcode 11.6 Swift 5.2.4

Last updated on August 12, 2020

So, if you’re using a class, structure, or enumeration which did not provide a method that would be very helpful for your app, you could simply add it via an extension (which you can read more about in the posts Swift Extensions Part 1, and Swift Extensions Part 2).  With extensions, you can easily add methods to a type that builds upon the existing types.  One of my favorites is writing my own UIColor convenience initializer (shown in the post How to Create a UIColor in Swift), that takes a value between 0 and 255, and hardcodes the alpha to 100% opacity.  I definitely understand why the authors did it the way they did, it is general so you can do many things with it, but I didn’t want the boilerplate required to convert my 0-255 based RGB values, and I CERTAINLY didn’t want to set the alpha to 100% each time when it would never be anything else in my app.

Well, that’s great and all for classes, structures, and enumerations…. but what about protocols?  In Swift 1, you could write your own protocol and have methods in it that would then have to be implemented by those types that adopt the protocol… but if it doesn’t have a method you need, the only thing you could do was either write an extension for each type that adopted the protocol, or write it as a global function.  That’s exactly what Apple did for a lot of functions in Swift 1, particularly many that worked with several CollectionTypes like Arrays, Dictionaries, and Sets.

With the coming of Swift 2 though, we now have a recourse that no longer requires global functions.  Now you CAN extend protocols with Protocol Extensions.  You can even provide default implementations for these methods defined in those protocol extensions!

[Read more…]

Filed Under: Swift Tagged With: Swift

Println Is Now Print, and Do-while Is Now Repeat-While in Swift

Xcode 11.6 Swift 5.2.4

Last updated on August 12, 2020

There are a few differences from Swift 2 that probably don’t need their own post, but should be mentioned nonetheless because they do have significant implications.  This post will cover a few of those changes:  Print replacing Println and the Repeat-While loop replacing the Do-While loop.
[Read more…]

Filed Under: Swift Tagged With: Swift

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

Pattern Matching in Swift

Xcode 11.6 Swift 5.2.4

Last updated on August 12, 2020

Pattern matching is a staple of functional languages.  At its core, Swift is primarily an object-oriented language, like Objective-C is.  However, there are many advantages to the way more functional style languages like Haskell and Erlang do things that the designers of Swift decided to include.  Pattern matching in particular, saves us having to type much longer, and less readable statements to do the same checks.

I mean, which is easier to read:

case (_, 0, 0):

or

if (someTuple.1 == 0) && (someTuple.2 == 0)

They both will find the same thing, but one can be used in a switch statement, and the other has to dig into the internals of a tuple and write a much longer looking comparison to 0 for each.  Not to mention the logical && there, to make sure both of them are true.
[Read more…]

Filed Under: Swift Tagged With: Swift

Integers and Numeric Literals in Swift

Xcode 11.6 Swift 5.2.4

Last updated on August 12, 2020

Numbers are the building blocks of any piece of software.  It eventually gets all the way down to our code being translated into a series of numbers that refer to specific CPU instructions and data to use with them.  We will stay well above that level in this post, but I thought it would be useful to share some of the special aspects of how numbers, particularly Integers, are handled in Swift.
[Read more…]

Filed Under: Swift Tagged With: Swift

  • « Previous Page
  • 1
  • 2
  • 3
  • 4
  • …
  • 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