Understanding Swift’s Opaque Types
Useful for SwiftUI
--
Before we start
Difficulty: Beginner | Easy | Normal | Challenging
This article has been developed using Xcode 12.2, and Swift 5.3
Prerequisites
* You will be expected to be aware how to make a Single View Application in Swift, or be able to use Playgrounds to be able to code in Swift
* I lean rather heavily on Generics
* Access Control is briefly touched upon
* Some knowledge of Protocols would be useful
* Equatable is used later in the article
Keywords and Terminology
Opaque Types: A data type whose concrete data structure is not defined in an interface
This project
Background
SwiftUI makes use of the some
keyword, as
The (rather simplistic) explanation is that the some
keyword indicates that the body
property has an opaque type
, that is obstruficating type information contained in the body
(we can think of the type information as being private). body
represents a class that implements View
. You need a concrete object for the View
, but it doesn’t matter which View
is used rather the capabilities of some View
are needed.
The Idea of a Reverse Generic type
Generic types let the code that calls a function type for that function’s parameters and return a value in a way this is abstracted from the function implementation.
Let us look at the following Generic