April 2026
Intermediate
683 pages
25h 29m
English
So far, we’ve worked through examples that extend custom data types. But here’s a surprise: Swift lets you extend its built-in data types too! Just like custom types, you can add computed properties, functions, initializers, protocol conformance, subscripts, and operators to standard types like String or Double. You can also extend library-specific data types, such as SpireKit SKScene or SwiftUI View. This section will teach you how to create such extensions and highlight some typical pitfalls.
As an example, Listing 11.22 features a String extension, adding a computed property and a function to the built-in type.
extension String { var isEmail: Bool { return self.contains("@") && self. ...
Read now
Unlock full access