IOS 09 R.swift框架和使用方法

R.swift框架主要是实现通过类字段访问字符串,图片,等资源;类似Android那边通过R类访问,好处是有提示,如果缺少资源,直接就是编译错误;OC类似的功能叫R.objc。

添加依赖

复制代码
添加依赖
#将资源(图片,文件等)生成类,方便到代码中方法
#例如:let icon = R.image.settingsIcon()
#let font = R.font.sanFrancisco(size: 42)
#let color = R.color.indicatorHighlight()
#let viewController = CustomViewController(nib: R.nib.customView)
#let string = R.string.localizable.welcomeWithName("Arthur Dent")
#https://github.com/mac-cain13/R.swift
pod 'R.swift'

还不了解如何使用 CocoaPods 管理依赖的,建议先看前面的文章:IOS 01 CocoaPods 安装与使用

添加完依赖后,看一下Pods文件夹里面是否添加成功。

配置

1.选择targets,在Build Phases标签,添加一个New Run Script Phase脚本。

2.拖拽新的脚本Run Script 到Compile Sources之前。

3.然后写入如下内容:

"PODS_ROOT/R.swift/rswift" generate "SRCROOT/R.generated.swift"

4.添加 $SRCROOT/R.generated.swift 到 "Output Files"

5.取消勾选的 "Based on dependency analysis",以便 R.swift 在每次构建时运行

6.编译项目,就可以看到生成的R.generated.swift文件

7.拖拽R.generated.swift文件到项目根目录,取消勾选Copy items if needed,点击Finish。

使用

添加了字符串,图片等资源后,一定要编译才能访问到。

Swift 复制代码
//使用框架
copyrightView.text=R.string.localizable.clickReload("网络错误")

bannerView.image = R.image.splashBanner()
相关推荐
蒙小萌199312 小时前
Performance and metrics
ios
shughui13 小时前
APP、Web、H5、iOS与Android的区别及关系
android·前端·ios
Edward.W15 小时前
iOS 17+真机命令行操作对照表
macos·ios·cocoa
2501_916008892 天前
iOS开发APP上架全流程解析:从开发到App Store的完整指南
android·ios·小程序·https·uni-app·iphone·webview
2501_915909062 天前
Charles 抓不到包怎么办?iOS 调试过程中如何判断请求路径
android·ios·小程序·https·uni-app·iphone·webview
2501_916007472 天前
iOS和iPadOS文件管理系统全面解析与使用指南
android·ios·小程序·https·uni-app·iphone·webview
core5122 天前
使用 `ms-swift` 微调 Qwen3-VL-2B 详细指南
lora·微调·swift·qwen·qwen3·vl
core5122 天前
Swift SFT Qwen-VL LoRA 微调指令详解
lora·微调·swift·qwen·vl
2501_915921433 天前
iOS App 开发阶段性能优化,观察 CPU、内存和日志变化
android·ios·性能优化·小程序·uni-app·iphone·webview
游戏开发爱好者83 天前
在 iOS 开发、测试与上架过程中 如何做证书管理
android·ios·小程序·https·uni-app·iphone·webview