OOP and Functional Swift

Become a Subscriber

Featuring the power of Objective-C with the elegance of a modern scripted language, Swift ushers in a new era for Apple software development. When announced in June of 2014, and in the years since, Swift has taken the spotlight; receiving praise from developers of nearly all levels. With ongoing support from Apple, and immersion into the iOS and Mac development communities, Swift will likely push out Objective-C completely in a few short years, and could last as the leading language for development for the next decade or more.

In love with Object-Oriented? Prefer Functional or procedural programming? The standard library supports both Functional and Object Oriented programming paradigms, allowing you to embrace the pattern of programming that you prefer. The language works well in many different patterns with a mix-and-match approach commonly being used by developers in implementation. The key to this flexibility is packing in features that are key to different paradigms: interfaces, static typing, optionals, generics, closures, memory management, etc. These features allow a developer to use Swift in a very rigid or very loosely architected manner. To see some examples of the flexibility and power with examples, take a look at the list of design patterns in Swift.

Functional or Object Oriented?

This article guides you towards understanding broadly when it is best to use a specific paradigm. While both functional and object oriented methodologies are important, there are specific cases when one is clearly better than another. We have also published dedicated articles for a deeper dive into best practices in both object oriented and functional programming in Swift.

Regardless of your preferred approach, it is important to understand both paradigms well when working with Swift. The third-party libraries may favor one or the other, requiring you to understand the syntax of such, and some aspects of programming are clearly suited for OOP or functional approaches.

Threading Applications

If you have worked with javascript promises or node.js before, then you may be lead to think that everything can and should be done in an asynchronous thread. However, when working with a platform and device as complicated as iOS or mac OS, multithreading reigns king. Because of this, working with instances becomes extremely important, and should not be avoided. Class instances are an OOP construct, where a class is created and stored in memory and any updates to the instance from anywhere in the application may modify the instance. Let’s look at a real-world example where object oriented approaches are necessary. This is pseudo code for brevity:

let menuItems: [MenuItem] = []

// Async fetch is dispatched while the program continues to execute
async_api_fetch_items() 
{
  for item in api_fetch_menu_items()
  {
    menuItems[id] = item
  }
}

// Regardless of which result comes back first, 
// the menuItems instance is updated as the data is retrieved
database_fetch_items()
{
  for item in database_fetch_menu_items()
  {
    menuItems[id] = item
  }
}