R.swift & SwiftGen 资源使用指南

R.swift 和 SwiftGen 资源转换使用指南

R.swift (原始代码会打包到项目?)

  • Pod

    ruby 复制代码
    platform :ios, '12.0'
    target 'LBtest' do
      # Comment the next line if you don't want to use dynamic frameworks
      use_frameworks!
      pod 'R.swift'
    #  pod 'SwiftGen', '~> 6.0'
    end
  • pod install

  • 设置 执行脚本 TARGETS-->Build Phases-->New Run Script Phase

  • 添加如下 code

    sh 复制代码
    if [ -f "${PODS_ROOT}/R.swift/rswift" ]; then
        "${PODS_ROOT}/R.swift/rswift" generate "${SRCROOT}/R.generated.swift"
    else
        echo "warning: R.swift not found, run 'pod install' to install R.swift."
  • 拖动当前 Run Script Phase 到 Check pods Manifest.lock 和 Compile Sources 之间

  • 在Xcode 中 新建一个 R.generated.swift 文件 位置在 项目根目录 即 当前项目文件夹下

  • 使用事例

    Localizable.string

    swift 复制代码
    "name" = "姓名";
    "age" = "18";
    swift 复制代码
    let name = R.string.localizable.name()
    debugPrint(name)    
    let age = R.string.localizable.age()
    debugPrint(age)

SwiftGen (原始代码不会打包到项目)

  • Pod

    ruby 复制代码
    platform :ios, '12.0'
    target 'LBtest' do
      # Comment the next line if you don't want to use dynamic frameworks
      use_frameworks!
      #pod 'R.swift'
      pod 'SwiftGen', '~> 6.0'
    end
  • pod install

  • 设置 执行脚本 TARGETS-->Build Phases-->New Run Script Phase

  • 添加如下 code

    sh 复制代码
     [[ -f "${PODS_ROOT}/SwiftGen/bin/swiftgen" ]]; then
      ${PODS_ROOT}/SwiftGen/bin/swiftgen config run
    else "warning: SwiftGen is not installed. Run 'pod install --repo-update' to install it."
    fi
  • 拖动当前 Run Script Phase 到 Check pods Manifest.lock 和 Compile Sources 之间

  • 在项目根目录下创建 swiftgen.ym 文件

    sh 复制代码
    input_dir: LBtest
    output_dir: .
    strings:
      inputs: en.lproj
      filter: .+\.strings$
      outputs:
        - templateName: structured-swift5
          output: Localizable.swift
          params:
            enumName: bslocalizable
    #xcassets:
    #  - inputs: Logos.xcassets
    #    outputs:
    #      - templateName: swift5
    #        output: Logos-Constants.swift
    #        params:
    #          enumName: Logos
    #  - inputs:
    #      - Colors.xcassets
    #      - Images.xcassets
    #    outputs:
    #      - templatePath: Resources/my-assets-custom-template.stencil
    #        output: Assets-Constants.swift
  • 将Localizable.swift 添加到 Xcode 项目

  • 使用事例:

    Localizable.string

    swift 复制代码
    "name" = "姓名";
    "age" = "18";
    swift 复制代码
    let name = bslocalizable.name
    debugPrint(name)
    
    let age = bslocalizable.age
    debugPrint(age)
  • Homebrew

    sh 复制代码
    $ brew install swiftgen

    在项目根目录下创建 swiftgen.ym 文件

    swift 复制代码
    input_dir: LBtest
    output_dir: .
    strings:
      inputs: en.lproj
      filter: .+\.strings$
      outputs:
        - templateName: structured-swift5
          output: Localizable.swift
          params:
            enumName: bslocalizable
    #xcassets:
    #  - inputs: Logos.xcassets
    #    outputs:
    #      - templateName: swift5
    #        output: Logos-Constants.swift
    #        params:
    #          enumName: Logos
    #  - inputs:
    #      - Colors.xcassets
    #      - Images.xcassets
    #    outputs:
    #      - templatePath: Resources/my-assets-custom-template.stencil
    #        output: Assets-Constants.swift
  • 进入当前根文件夹下 打开终端执行run

    swift 复制代码
    swiftgen config run 
相关推荐
画个大饼10 小时前
Swift与iOS内存管理机制深度剖析
开发语言·ios·swift
烎就是我1 天前
100行代码swift从零实现一个iOS日历
ios·swift
littleplayer2 天前
iOS 中的 @MainActor 详解
前端·swiftui·swift
cauyyl2 天前
xcode 16 遇到contains bitcode
react native·xcode
A_ugust__2 天前
Vue3集成浏览器API实时语音识别
人工智能·语音识别·xcode
躺平每一天2 天前
用 Swift 的高阶函数 reduce 提升代码可读性
swift·掘金·金石计划
未来之窗软件服务2 天前
声音分离人声和配乐-从头设计数字生命第5课, demucs——仙盟创梦IDE
ide·macos·xcode·仙盟创梦ide·数字生命
lilili啊啊啊3 天前
iOS 应用性能测试工具对比:Xcode Instruments、克魔助手与性能狗
测试工具·ios·iphone·xcode·克魔
风浅月明5 天前
[Swift]pod install成功后运行项目报错问题error: Sandbox: bash(84760) deny(1)
swift
season_zhu5 天前
iOS开发:关于Moya之上的Request层
ios·架构·swift