R.swift 和 SwiftGen 资源转换使用指南
R.swift (原始代码会打包到项目?)
Pod
platform :ios, '12.0'target 'LBtest' do# Comment the next line if you don't want to use dynamic frameworksuse_frameworks!pod 'R.swift'#pod 'SwiftGen', '~> 6.0'end
pod install
设置 执行脚本 TARGETS–>Build Phases–>New Run Script Phase
添加如下 code
if [ -f "${PODS_ROOT}/R.swift/rswift" ]; then"${PODS_ROOT}/R.swift/rswift" generate "${SRCROOT}/R.generated.swift"elseecho "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
"name" = "姓名";"age" = "18";
let name = R.string.localizable.name()debugPrint(name)let age = R.string.localizable.age()debugPrint(age)
SwiftGen (原始代码不会打包到项目)
Pod
platform :ios, '12.0'target 'LBtest' do# Comment the next line if you don't want to use dynamic frameworksuse_frameworks!#pod 'R.swift'pod 'SwiftGen', '~> 6.0'end
pod install
设置 执行脚本 TARGETS–>Build Phases–>New Run Script Phase
添加如下 code
[[ -f "${PODS_ROOT}/SwiftGen/bin/swiftgen" ]]; then${PODS_ROOT}/SwiftGen/bin/swiftgen config runelse "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 文件
input_dir: LBtestoutput_dir: .strings:inputs: en.lprojfilter: .+\.strings$outputs:- templateName: structured-swift5output: Localizable.swiftparams: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
"name" = "姓名";"age" = "18";
let name = bslocalizable.namedebugPrint(name)let age = bslocalizable.agedebugPrint(age)
Homebrew
$ brew install swiftgen
在项目根目录下创建 swiftgen.ym 文件
input_dir: LBtestoutput_dir: .strings:inputs: en.lprojfilter: .+\.strings$outputs:- templateName: structured-swift5output: Localizable.swiftparams: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
swiftgen config run