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 
相关推荐
牛马11113 分钟前
ios swift处理json数据
ios·json·swift
Swift社区26 分钟前
LeetCode 473 火柴拼正方形 - Swift 题解
算法·leetcode·swift
大熊猫侯佩19 小时前
Swift 6.2 列传(第十七篇):钟灵的“雷电蟒”与测试附件
单元测试·swift·apple
大熊猫侯佩19 小时前
App 暴毙现场直击:如何用 MetricKit 写一份完美的“验尸报告”
app·xcode·apple
东坡肘子1 天前
AT 的人生未必比 MT 更好 -- 肘子的 Swift 周报 #118
人工智能·swiftui·swift
njsgcs3 天前
Swift playground 网页刷新切换随机页面的网页查看器WebKit
swift
声网4 天前
如何用 Fun-ASR-Nano 微调一个「听懂行话」的语音模型?丨Voice Agent 学习笔记
笔记·学习·xcode
桃子叔叔5 天前
基于SWIFT框架的预训练微调和推理实战指南之完整实战项目
大模型·swift
菜的不敢吱声5 天前
swift学习第5天
学习·ssh·swift
符哥20085 天前
Swift开发app常见第三方库
学习·swift