Nested Objects in Swift

Learn how to use nested objects in Swift and get the best out of them

Alessandro Manilii
4 min readDec 30, 2021

Swift is an amazing language and allows us to build up lots of incredible structures to write better code. One mildly used feature is the ability to define object types inside other objects in a nested way. Yes that’s true, you can create a ”matryoshka” like hierarchy in your code.

Let’s make an example.
Maybe in you app you need to create a structure that describe a rock band, with all the musicians playing in it. So you can create a Band struct that define internally an enum describing the instrument played by each member:

struct Band {    enum MusicianType {
case singer
case guitar
case keyboard
case bass
case drums
}
private var members: [MusicianType]
private (set) var name: String
init(name: String, members: [MusicianType]) {
self.name = name
self.members = members
}
var description: String {
“\(name) are made by \(members.count) members”
}
}

So now you can easily instantiate objects in this way:

let band01 = Band(
name: “Dream Theater”,
members: [.singer, .bass, .drums, .guitar, .keyboard]
)
let band02 = Band(
name: “Iron Maiden”,
members: [.singer, .bass, .guitar, .guitar, .guitar, .drums]
)

You could have defined the enum MusicianType outside the Band struct, but since it is “logically” tightly coupled to the owner structure, defining it inside Band makes it clearer and cleaner.

Remember also that in a medium size app you could end up with dozens and dozens of small objects used only in one other class, so maybe it’s time for you to think about nested types…

If you need to use the MusicianType elsewhere in your app anyway, you can call it like this:

let guitarPlayer = Band.MusicianType.guitar

…and you will always have clear that the case guitar is part of the Band struct.

Another way to use nested types is creating a struct for costants in another object.
Let’s say for instance that you have a sort of biometric manager and, according to the user behavior, you have to show some kind of message messages. You could create something like that:

final class PBBiometricAuthManager {  struct Constant {
static let appTitle = localized(“appTitle”
static let authRequired = localized(“authRequired”)
static let sessionCancelled = localized(“sessionCancelled”)
static let tryAgain = localized(“tryAgain”)
static let manualLogin = localized(“manualLogin”)
static let touchIDNotEnrolled = localized(“touchIDNotEnrolled”)
static let faceIDNotEnrolled = localized(“faceIDNotEnrolled”)
static let passcodeNotSet = localized(“passcodeNotSet”)
static let touchIDNA = localized(“touchIDNotAvailable”)
static let faceIDNA = localized(“faceIDNotAvailable”)
static let biometricSensorNA = localized(“noBiometricSensor”)
static let close = localized(“close”)
}
// Lots of code here…}

Defining the costants as static will allow us to call the easily without the need to instantiate the struct, just like that: Constant.appTitle

In future articles I will show you how to use nested types to write better and cleaner code!

In the meanwhile… enjoy your coding!!!

If you liked this article and you are not a Medium member, please consider to join it. Using my referral link will help me to write better and better articles.

How to run Xcode 12 on macOS Monterey

With the latest major release of macOS, it came a surprise… you are not longer able to run Xcode 12! If you click on the app, a warning popup will come up stopping the execution of the IDE.
In this period I’m working for an app for a big client that, for many reasons, cannot run con Xcode 13, and for me it was a huge problem… if you are experiencing the same issue, keep reading because I have a simple solution for you.

Just follow these steps:

  1. Find the position of the Xcode 12 app on Finder.
  2. Open the Terminal App.
  3. Drag and Drop the Xcode App icon on the Terminal and you will see the path of the file. You will see something similar: /Users/YOURNAME/Developer/Xcode12.app/
  4. Add after the / this command Contents/MacOS/Xcode
  5. Execute the command and enjoy Xcode 12.

You will have something similar:

In order to let Xcode run you will have to leave the terminal window opened and untouched. If you need to use it, open another instance or another tab with Command+T .

This is my Patreon page. Subscribe and support me if you like.

--

--

Alessandro Manilii

I’m an Italian professional iOS Developer, iOS Tech Lead at Wakala — Join Medium from the following link: https://medium.com/@alessandromanilii/membership