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 
相关推荐
0xAaron2 小时前
ips 文件符号化
ios·swift·调试·ips·符号化
HarderCoder3 小时前
脱离 SwiftUI 也能用 @Observable:深入理解 withObservationTracking 的玩法、坑点与 Swift 6 突围
swift
kk哥889915 小时前
Swift底层原理学习笔记
笔记·学习·swift
confiself16 小时前
通义灵码分析ms-swift框架中CHORD算法实现
开发语言·算法·swift
1024小神16 小时前
在 Swift 中,self. 的使用遵循明确的规则
开发语言·ios·swift
Swift社区16 小时前
Swift 类型系统升级:当协议遇上不可拷贝的类型
开发语言·ios·swift
小小8程序员21 小时前
swift的inout的用法
开发语言·ios·swift
南玖i1 天前
vue2/html 实现高德点聚合
开发语言·ios·swift
Haha_bj2 天前
Swift ——详解Any、AnyObject、 AnyClass
ios·swift