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()
相关推荐
2501_915106329 分钟前
苹果软件加固与 iOS App 混淆完整指南,IPA 文件加密、无源码混淆与代码保护实战
android·ios·小程序·https·uni-app·iphone·webview
2501_9159214317 分钟前
iOS 26 崩溃日志解析,新版系统下崩溃获取与诊断策略
android·ios·小程序·uni-app·cocoa·iphone·策略模式
林鸿群2 小时前
Apple M3 MacOS arm64 编译QGroundControl5.0.8(base on Qt 6.8.3)
macos·ios·qgc·qgroundcontrol
2501_916013743 小时前
iOS 推送开发完整指南,APNs 配置、证书申请、远程推送实现与上架调试经验分享
android·ios·小程序·https·uni-app·iphone·webview
2501_915909068 小时前
HTML5 与 HTTPS,页面能力、必要性、常见问题与实战排查
前端·ios·小程序·https·uni-app·iphone·html5
JZXStudio9 小时前
4.布局系统
框架·swift·app开发
2501_9151063212 小时前
JavaScript编程工具有哪些?老前端的实用工具清单与经验分享
开发语言·前端·javascript·ios·小程序·uni-app·iphone
HarderCoder14 小时前
Swift 函数完全指南(四):从 `@escaping` 到 `async/await`——打通“回调→异步→并发”任督二脉
swift
HarderCoder14 小时前
Swift 函数完全指南(三):`@autoclosure`、`rethrows`、`@escaping` 与内存管理
swift