…proper implementation of a string type in Swift has been a controversial topic for quite some time. The design is a delicate balance between Unicode correctness, encoding agnosticism, ease-of-use and high-performance. Almost every major release of Swift has refined the String type to the awesome design…
iOS & Swift
Chapter in Expert Swift
Codable
Jun 11 2025 · Chapter
…different formats using various encoders and decoders. JSON data Plist data Custom data JSONEncoder PropertyListEncoder PropertyListDecoder Custom encoders Custom decoders PERSON id: UUID name: String address: struct favorite: enum Conforms to Codable JSONDecoder What you’ll learn, and what you won’t Because this is an advanced book…
iOS & Swift
Chapter in Expert Swift
Higher-Order Functions
Jun 11 2025 · Chapter
…text printer. Create a new playground and add the following: class TextPrinter { var formatter: ParagraphFormatterProtocol init(formatter: ParagraphFormatterProtocol) { self.formatter = formatter } func printText(_ paragraphs: [String]) { for text in paragraphs { let formattedText = formatter.formatParagraph(text) print(formattedText) } } } protocol ParagraphFormatterProtocol { func formatParagraph(_ text: String) -> String } The code above is for a text printer…
iOS & Swift
Chapter in Expert Swift
API Design Tips & Tricks
Jun 11 2025 · Chapter
…base: Encrypted } Then, imagine you have a Person struct with a private property called password: struct Person { let id: UUID let name: String private let password: String } If you extend Encrypter in the same file to provide encryption for Person, like so: extension Encrypter where Encrypted == Person { func encrypt…
iOS & Swift
Chapter in Expert Swift
Protocols
Jun 11 2025 · Chapter
…your protocols: struct Text: ImmutableLocalizable { static let supportedLanguages: [Language] = [.english, .croatian] var content = "Help" func changed(to language: Language) -> Self { let newContent: String switch language { case .english: newContent = "Help" case .german: newContent = "Hilfe" case .croatian: newContent = "Pomoć" } return Text(content: newContent) } } The Text struct implements the ImmutableLocalizable protocol. This…
iOS & Swift
Chapter in Expert Swift
Generics
Jun 11 2025 · Chapter
…replace the contents of fetchArticles with the following: let baseURL = "https://api.kodeco.com/api" let path = "/contents?filter[content_types][]=article" let url = URL(string: baseURL + path)! do { // 1 let articlesData = try await networker.fetch(url: url) // 2 let decoder = JSONDecoder() let decodedArticles = try decoder .decode(Articles.self, from: articlesData).data articles = decodedArticles.map…
iOS & Swift
Chapter in Expert Swift
Sequences, Collections & Algorithms
Jun 11 2025 · Chapter
…looks like: May consume elements Sequence Access by index Collection Iteration state IteratorProtocol Change elements MutableCollection Forward, backward traversal BidirectionalCollection Change subranges RangeReplaceableCollection Unify String and Substring StringProtocol Random access traversal RandomAccessCollection vends Sequence types protocol hierarchy The hierarchy includes: Sequence - This is the most primitive type in the hierarchy…
iOS & Swift
Chapter in Expert Swift
Instrumentation
Jun 11 2025 · Chapter
…infoWriter = InformationWriter(writer: self) infoWriter?.doSomething() } Moreover, MemoryLeaksViewController conforms to the protocol WriterProtocol, which only defines the method writeText(_:): protocol WriterProtocol { func writeText(_ text: String) } Now, look at InformationWriter.swift. Its init takes a writer as the parameter and creates a closure that calls writeText(_:) on that writer: init(writer: WriterProtocol…
iOS & Swift
Chapter in Expert Swift
Types & Mutation
Jun 11 2025 · Chapter
…particular situation. As discussed in the previous chapter, it’s pretty incredible that all the standard library types such as Bool, Int, Double, String, Optional, Array and Dictionary are clever compositions of these fundamental types. It speaks to the power of what you can do with them. Note: Protocols…
iOS & Swift
Chapter in Expert Swift
Numerics and Ranges
Jun 11 2025 · Chapter
…functionality and codifies their two’s complement representation, which you’ll learn about shortly. But there’s more. Integers losslessly convert to and from String types thanks to additional protocols and methods. The protocol relationships look like this: min, max, endian, wrapping, random, ... FixedWidthInteger bitWidth, bitwise, %, ... BinaryInteger init? LosslessStringConvertible description…
iOS & Swift
Chapter in Expert Swift
Objective-C Interoperability
Jun 11 2025 · Chapter
…generics, which support a relatively large set of generic scenarios, such as bridging Array<FeedItem> to NSArray <FeedItem *>*, Dictionary<String, Any> to NSDictionary<NSString*, id>* and vice versa. It will even keep generic constraints of your own Objective-C classes…
iOS & Swift
Chapter in Expert Swift
Introduction
Jun 11 2025 · Chapter
…being multi-paradigm. Apple demonstrated Swift’s modern language features at that first conference (WWDC14). In the first live demo, they functionally mapped strings to SpriteKit images. In addition to handling the intricacies of giant, legacy object-oriented frameworks, such as UIKit, CoreData and SpriteKit, Swift can also speak functionally…
iOS & Swift
Chapter in Expert Swift
Functional Reactive Programming
Jun 11 2025 · Chapter
…failure, and a completion event indicates a valid and expected completion of the stream. Infinite float stream, never completes Boolean stream, ends with completion String stream, ends with failure 0 8.2 42 83 false false true false eel act ace rag Thinking of water Streams of data are analogous…
iOS & Swift
Chapter in Expert Swift
Unsafe
Jun 11 2025 · Chapter
This means you cannot access uninitialized memory and can only treat an instance as the type it was created. You can’t treat a String as if it were an Int or a Numeric and vice-versa. But this doesn’t cover completely what the word safe means…
iOS & Swift
Chapter in Expert Swift
Introduction
Jun 11 2025 · Chapter
…intimately familiar with protocols and generics. Section II: Standard Library This section covers the base layer of writing Swift programs: Numerics, Ranges, Strings, Sequences, Collections, Codable and the less obvious, but very important topic - Unsafe. As you’d expect from an advanced book, we don’t only explain these topics…