# Swift & runtime

Language: en
Canonical: [Swift & runtime](<https://allluckly.cn/en/journal/topics/swift-runtime/>)
中文：[中文 HTML](<https://allluckly.cn/journal/topics/swift-runtime/>)
English: [English HTML](<https://allluckly.cn/en/journal/topics/swift-runtime/>)

DEVELOPMENT NOTES · 15 articles

From Swift 2 and Swift 3 notes to the Objective-C runtime and design patterns, these articles preserve the process of understanding language behaviour. Syntax changes; asking why a piece of code executes this way is still useful.

## How to read this collection

1. Start with delegation, blocks and common Swift pitfalls. Distinguish values, references, closure captures and object lifetimes.
2. For runtime articles, work through properties and ivars, messaging, then dictionary-to-model conversion. Separate what the runtime can do from what your project actually needs.
3. Compare templates, singletons, flyweights and responsibilities against a concrete duplication or shared-state problem before choosing a pattern.

These notes date from 2015–2017. Their examples have not been rerun for this archive update. Check the dates and version notes before applying the ideas to a current project.

## Articles in this topic

15 articles · newest first

- 2016-11-07 

  ### [Bitwise Operations in iOS](<https://allluckly.cn/en/journal/2016-11-07-bitwise-operations-in-ios/>)

   

  Introduces bitwise AND, OR, XOR, and NOT, using packed RGBA colors and state flags as examples. It then explains how enumerations and masks can store and query several states together.

   

  Sindri bitwise operations · bit masks · RGBA
- 2016-10-27 

  ### [Balancing Code Responsibilities in iOS](<https://allluckly.cn/en/journal/2016-10-27-balancing-code-responsibilities-in-ios/>)

   

  Uses an example project to examine oversized MVC controllers and the responsibilities of views, models, and controllers. It extracts repeated logic and reassigns functionality, emphasizing clear ownership of code before switching architecture labels.

   

  Sindri MVC · separation of responsibilities · refactoring
- 2016-10-13 

  ### [Swift 3.0 Design Patterns: Singleton](<https://allluckly.cn/en/journal/2016-10-13-swift-3-0-design-patterns-singleton/>)

   

  Introduces the uses and trade-offs of Swift singletons, creating shared instances with a static class constant or a global constant. It covers private initializers, access across modules, and the need to consider thread safety for shared mutable state.

   

  Sindri Swift 3.0 · singleton pattern · static let
- 2016-10-13 

  ### [Swift 3.0 Design Patterns: Flyweight](<https://allluckly.cn/en/journal/2016-10-13-swift-3-0-design-patterns-flyweight/>)

   

  Introduces the flyweight pattern through shared local-data managers. A dictionary reuses loaded data and tracks usage counts; unused data is saved and released. The example contrasts this lifecycle with a singleton that remains in memory.

   

  Sindri Swift 3.0 · flyweight pattern · object reuse
- 2016-09-01 

  ### [Protocols and Delegation in iOS](<https://allluckly.cn/en/journal/2016-09-01-protocols-and-delegation-in-ios/>)

   

  Uses UITableView and custom controls to distinguish protocols from delegation. It compares delegate and block callbacks, then discusses how data sources and protocol abstractions can reduce coupling between objects.

   

  Sindri protocol · delegate · block
- 2016-08-16 

  ### [Objective-C Runtime in Practice: Mapping Dictionaries to Models](<https://allluckly.cn/en/journal/2016-08-16-objective-c-runtime-in-practice-mapping-dictionaries-to-models/>)

   

  Moves from manual assignment, KVC, and third-party model libraries to an NSObject category that traverses runtime information and maps dictionaries to models. It also defines the example's support for two levels of nested data.

   

  Bison Objective-C Runtime · dictionary-to-model mapping · KVC
- 2016-07-28 

  ### [Decoupling iOS Views from Concrete Model Types](<https://allluckly.cn/en/journal/2016-07-28-decoupling-ios-views-from-concrete-model-types/>)

   

  Uses several types of ticket data with different fields but similar presentation needs to explore a dictionary-backed approach. A protocol exposes common display properties, reducing cells' and controllers' dependence on specific models and clarifying where data conversion belongs.

   

  Sindri protocols · model decoupling · MVC
- 2016-06-29 

  ### [Swift in practice: the template pattern](<https://allluckly.cn/en/journal/2016-06-29-swift-in-practice-the-template-pattern/>)

   

  Uses product data and calculations to examine code that depends on a tuple's structure. Types and protocols then organise reusable processing, showing how abstraction can reduce coupling between parameter formats and business methods.

   

  Sindri Swift · template pattern · protocols
- 2016-05-20 

  ### [Reading the Objective-C runtime source to understand messaging](<https://allluckly.cn/en/journal/2016-05-20-reading-the-objective-c-runtime-source-to-understand-messaging/>)

   

  Uses an unrecognized selector error to introduce Objective-C messaging, examining method lookup, caching and forwarding in the source. Runtime examples then explain how a method call is resolved during execution.

   

  Sindri Objective-C Runtime · message sending · method cache
- 2016-05-04 

  ### [Objective-C runtime in practice: changing fonts throughout an interface](<https://allluckly.cn/en/journal/2016-05-04-objective-c-runtime-in-practice-changing-fonts-throughout-an-interface/>)

   

  Starts with registering a custom font and finding its name, then uses Objective-C runtime techniques to replace interface fonts. It discusses the differences between controls created in code and those loaded from a storyboard when applying a common setting.

   

  HenryCheng Objective-C Runtime · custom fonts · UIFont
- 2016-04-06 

  ### [Understanding blocks in iOS development](<https://allluckly.cn/en/journal/2016-04-06-understanding-blocks-in-ios-development/>)

   

  Starts with block declarations and calls, then examines captured variables and callback wrappers. Examples modelled on Swift's higher-order functions demonstrate how blocks can be used to process data in Objective-C.

   

  Sindri block · variable capture · Objective-C
- 2016-03-01 

  ### [iOS refactoring notes: Extract Method](<https://allluckly.cn/en/journal/2016-03-01-ios-refactoring-notes-extract-method/>)

   

  Applies the Extract Method technique from Refactoring to split long stretches of code into functions organised by intent. Parameters, local variables and return values are handled step by step, with examples showing how to reduce repetition and improve readability.

   

  SindriLin refactoring · Extract Method · function extraction
- 2016-02-16 

  ### [Seven Swift pitfalls and how to avoid them](<https://allluckly.cn/en/journal/2016-02-16-seven-swift-pitfalls-and-how-to-avoid-them/>)

   

  Uses code examples to investigate surprising behaviour in protocol extensions, optional-chain assignment and functional processing. It compares dispatch for value and reference types and explains how code can compile successfully while producing an unexpected result.

   

  bestswifter Swift · protocol extensions · optional chaining
- 2016-01-16 

  ### [Inside KVO on iOS](<https://allluckly.cn/en/journal/2016-01-16-inside-kvo-on-ios/>)

   

  Starts with observing a list's scroll offset, then examines the dynamic subclass and isa-pointer changes caused by registering an observer. Runtime functions are used to attempt a similar mechanism, connecting practical KVO usage with its underlying behaviour.

   

  SindriLin KVO · isa · dynamic subclasses
- 2016-01-10 

  ### [Objective-C runtime fundamentals: properties and instance variables](<https://allluckly.cn/en/journal/2016-01-10-objective-c-runtime-fundamentals-properties-and-instance-variables/>)

   

  Distinguishes Objective-C properties from instance variables, examining runtime structures, memory offsets and synthesize to explain how properties are generated. Examples then show why dynamically inspecting and manipulating an object's structure can be useful.

   

  SindriLin Objective-C Runtime · property · ivar

