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 
相关推荐
孚亭7 小时前
Swift添加字体到项目中
开发语言·ios·swift
YGGP7 小时前
【Swift】LeetCode 76. 最小覆盖子串
swift
taokexia16 小时前
SwiftUI 组件开发: 自定义下拉刷新和加载更多(iOS 15 兼容)
ios·swift
qixingchao20 小时前
iOS Swift 线程开发指南
ios·swift
HarderCoder2 天前
Swift 扩展(Extension)指南——给现有类型“加外挂”的正规方式
swift
HarderCoder2 天前
【Swift 错误处理全解析】——从 throw 到 typed throws,一篇就够
swift
HarderCoder2 天前
【Swift 并发编程入门】——从 async/await 到 Actor,一文看懂结构化并发
swift
2501_916007473 天前
从零开始学习iOS App开发:Xcode、Swift和发布到App Store完整教程
android·学习·ios·小程序·uni-app·iphone·xcode
HarderCoder3 天前
Swift 中的不透明类型与装箱协议类型:概念、区别与实践
swift
HarderCoder3 天前
Swift 泛型深度指南 ——从“交换两个值”到“通用容器”的代码复用之路
swift