Static Library and Swift

前端 未结 4 507
别跟我提以往
别跟我提以往 2020-11-28 06:47

So I\'m working on an iOS project in Swift, and I wanted to create a Static library with some useful stuff in it.

My problem is when I try to build my lib in Xcode (

4条回答
  •  情歌与酒
    2020-11-28 07:24

    Swift consumer -> Swift static library

    Xcode version 10.2.1

    Create Swift static library

    Create a library project or create a library target

    File -> New -> Project... -> Cocoa Touch Static Library
    //or
    Project editor -> Add a Target -> Cocoa Touch Static Library 
    

    Add files .swift

    Select `.swift` file -> Select File Inspectors Tab -> Target Membership -> Select the target
    //or
    Project editor -> select a target -> Build Phases -> Compile Sources -> add files
    

    Build library - ⌘ Command + B or Product -> Build

    Note 1: Be sure that you build library for the same process architecture as the client code.
    Note 2: expose your API that should be visible for consumer using public or open access modifiers[About]

    Find generated output[Build location]

    Products group -> lib.a -> Show in Finder
    

    The directory includes

    • lib.a – a built static library
    • .swiftmodule. swiftmodule describe an interface of a library and a compiler version. This folder includes:
      • .swiftdoc - docs
      • .swiftmodule - public interface/definitions

    Swift consumer with Swift static library

    Drag and drop the binary into the Xcode project[About]

    Link Binary[Undefined symbols] [Link vs Embed]

    Project editor -> select a target -> General -> Linked Frameworks and Libraries -> add -> Add Others... -> point to `lib.a` file
    //or
    Project editor -> select a target -> Build Phases -> Link Binary With Libraries -> add -> Add Others... -> point to `lib.a` file
    

    Add Library Search paths[Library not found for] [Recursive path]

    Project editor -> select a target -> Build Settings -> Search Paths -> Library Search paths -> add path to the parent of `lib.a` file
    

    Add Import Paths[No such module] [Recursive path]

    Project editor -> select a target -> Build Settings -> Swift Compiler - Search Paths -> Import Paths -> add path to a folder with `.swiftmodule`
    

    Import module to the Swift client code [module_name]

    import module_name
    

    [More examples]

提交回复
热议问题