Upgrade to Pro — share decks privately, control downloads, hide ads and more …

iOS Module Intro

CJ
August 17, 2022

iOS Module Intro

CJ

August 17, 2022
Tweet

More Decks by CJ

Other Decks in Programming

Transcript

  1. Topics covered • #include #import • .pch • Clang Module

    ◦ pcm ◦ .modulemap • Swift Module ◦ .swiftmodule ◦ .swiftinterface • Umbrella header • Umbrella framework
  2. #import at ObjC • No need #include guard • Clang

    determines if this header has been included already ◦ avoids duplicate inclusion
  3. #import at ObjC • Compile-time scalability ◦ N translation units

    ◦ M headers ◦ In each translation unit, the compiler is performing M x N work ◦ Even though most of the M headers are shared among multiple translation units • Translation unit ◦ consists of a source file after it has been processed by the C preprocessor, meaning that header files listed in #include directives are literally included
  4. Pre Compiled Header • .pch • Precompile that bundle of

    headers into a single precompiled header (PCH file). • Then, when compiling the source files in the project, we load the PCH file first. • Drawback ◦ Every .m can access the .pch
  5. Clang Module • Binary .pcm • Usage ◦ @import ◦

    And also #include and #import after turn module ON
  6. Clang Module • Problems Clang Module do NOT solve: ◦

    Naming collistion in the only one namespace
  7. Clang Module - modulemap Defines • Umbrella Header: UIKit.h •

    Sub module which needs to export: All • Linked framework: UIKit
  8. Umbrella Header • Umbrella: something that includes or represents a

    group or range of similar things. 綜合體 (from Cambridge Dictionary) • Master header for a framework
  9. Umbrella Framework (by the way…) • Duplicate • Difficult for

    dependency and version control • Might only suitable if all the frameworks are custom frameworks
  10. Swift Module • The closest analogy to C-style header files

    in Swift is the .swiftmodule file • Binary format (LLVM bitcode) file that records the declarations (types, function signatures, etc.) • When you import that module from another module that you're compiling, `swiftc` loads that `.swiftmodule` file into memory so that it can typecheck in that module. • Has namespace in module
  11. Swift Module • Internal compiler data structures ◦ Contains Abstract

    Syntax Tree(AST) ◦ Swift Intermediate Language(SIL) of framework’s public API.
  12. .swiftinterface • Includes the version of the compiler that produced

    this interface • Also contains the subset of command line flags that the Swift Compiler needs to import this as a module.
  13. .swiftinterface • Include the public API, but not private •

    class has a de-initializer in the interface. If don't provide an explicit de-initializer, the compiler generates one
  14. .swiftinterface if you make an enum without any associated values,

    then the compiler implicitly makes that conform to an Equitable and Hashable
  15. ObjC Interface of Swift Framework • Xcode's default template ◦

    Objective-C Umbrella Header ◦ Generated header ▪ containing the Objective-C parts in the framework
  16. ObjC Interface of Swift Framework • Generated header: Framework-Swift.h ◦

    containing the Objective-C parts in the framework