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 
相关推荐
Mamong11 小时前
Metal 学习笔记三:渲染管线
ios·swift·metal
逐星ing2 天前
【AIGC】使用Python实现科大讯飞语音服务ASR转录功能:完整指南
python·aigc·xcode
DeMonnnnnn2 天前
Flutter-升级Xcode后构建iOS报错
flutter·ios·xcode
明远湖之鱼2 天前
关于我在 iOS 开发中用 Swift 实现的两个交互效果
前端·ios·swift
TrueDei4 天前
Xcode如何高效的一键重命名某个关键字
xcode
闲猫6 天前
go WEB框架
前端·golang·web·xcode
B.-6 天前
在 Flutter 中实现文件读写
开发语言·学习·flutter·android studio·xcode
9144062327 天前
xcode打包导出ipa
ide·macos·xcode
coooliang7 天前
【iOS】SwiftUI状态管理
ios·swiftui·swift
席子哥哥的代码库9 天前
自制简单的图片查看器(python)
开发语言·python·swift