Coding Explorer Blog

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

  • Home
  • Apps
  • About
  • Contact

Protocols in Swift

Xcode 11.6 Swift 5.2.4

Last updated on August 10, 2020

I wanted to write a bit about Generics in Swift, but I realized that some of the major powers of Generics require the use of protocols, so I felt I should start by talking about them first.

In Swift, protocols are basically a named contract that your classes can conform to.  If your class says it conforms to Equatable, then it better fulfill all of the required functionality to make it equatable.  They are rather similar to interfaces in C# or Java.  They list off the function prototypes and variable declarations, as well as stating whether they are required or optional, but don’t actually do anything.  It is up to class, structs, or enumerations that claim to conform to the protocol to actually provide the functionality.

Defining a Protocol

This is pretty easy.  It is very much like defining a class in Swift, except you write protocol instead, observe:

protocol Vehicle {
    //Functions and properties prototypes go here.
}

Then you just fill it with what you want.
[Read more…]

Filed Under: Swift Tagged With: optionals, properties

Computed Properties in Swift

Xcode 11.6 Swift 5.2.4

Last updated on August 10, 2020

Continuing the discussion about variables in Swift, let us move on to properties, which are basically Swift constants or variables used in a class, structure, or enumeration.  More specifically, they are stored values for a class, structure, or enumeration, that have getter and setter methods that can be modified.  You can leave them alone, and they will do what they should (return the value or set a new one).  The getters and setters can be overridden, like to override the setter to allow you to check if the input value is valid before storing it.  Getters and setters are also called “accessors” and “mutators” respectively.  Take your pick, but for me, I’m going to just call them getters and setters.

There are a few more aspects to properties, but today, we will be talking about a specific type of property. [Read more…]

Filed Under: Swift Tagged With: properties

  • « Previous Page
  • 1
  • 2

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