R.swift:以一种优雅安全的方式使用资源文件

https://github.com/zhiguangqiao/R.swift


先来看下目前如果我们要使用资源文件代码是如何调用的:

1
2
3
leticon=UIImage(named: "settings-icon" )
letfont=UIFont(name: "SanFrancisco" ,size:42)
performSegueWithIdentifier( "openSettings" )

这种通过传入字符串来获取资源有很大的潜在的风险:

  • 最常见的就是资源名称拼写错误。如果项目资源很多检查拼写正确也是颇费时间

  • 如果删除一个资源文件,只能通过全局搜索资源名称来判断是否已经没有使用这个资源

R.swift解决方

先看下上面的逻辑用R.swift代码调用

leticon=R.image.settingsIcon
letfont=R.font.sanFrancisco(size:42)
performSegueWithIdentifier(R.segue.openSettings)

R如何解决上面的问题:

  • 强类型

使用一个资源前,先声明是什么类型。如果是一个图片资源就是R.image.xx。这样每次明确知道使用的资源类型。(swift是一门强类型语言,强类型的一个好处就是很多错误可以在编译时就发现)

因为会自动根据资源文件生成结构体,所以可以直接使用,不用自己拼写资源名

支持的资源类型

3
4
5
6
//使用R.swift之前
letsettingsIcon=UIImage(named: )
letgradientBackground=UIImage(named: "gradient.jpg" )
//使用R.swift
letsettingsIcon=R.image.settingsIcon
letgradientBackground=R.image.gradientJpg

6
7
8
9
10
11
12
13
letstoryboard=UIStoryboard(name: "Main" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,bundle:nil)
letinitialTabBarController=storyboard.instantiateInitialViewController()as?UITabBarController
letsettingsController=self.instantiateViewControllerWithIdentifier( "settingsController" )as?SettingsController
//使用R.swift
letstoryboard=R.storyboard.main.instance
letinitialTabBarController=R.storyboard.main.initialViewController
letsettingsController=R.storyboard.main.settingsController
//通过这个代码来校验运行时storyboard的图片是否都能被加载
//只在debug模式下有效,会通过断言来提示
R.storyboard.main.validateImages()
//在运行时校验所有的viewController能够被正常加载
mode.R.storyboard.main.validateViewControllers()
4
)
//使用R.swift
Nibs

12
letnameOfNib= "CustomView"
letcustomViewNib=UINib(nibName: "CustomView" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,bundle:nil)
letrootViews=customViewNib.instantiateWithOwner(nil,options:nil)
letcustomView=rootViews[0]as?CustomView
letviewControllerWithNib=CustomViewController(nibName: ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,bundle:nil)
//使用R.swift
letnameOfNib=R.nib.customView.name
letcustomViewNib=R.nib.customView
letrootViews=R.nib.customView.instantiateWithOwner(nil,options:nil)
letcustomView=R.nib.customView.firstView(nil,options:nil)
letviewControllerWithNib=CustomViewController(nib:R.nib.customView)
7
lettextCellNib=UINib(nibName: "TextCell" tableView.registerNib(textCellNib,forCellReuseIdentifier: "TextCellIdentifier" tableView.registerNib(R.nib.textCell)
//cellForRowAtIndexPath中获取cell
lettextCell=tableView.dequeueReusableCellWithIdentifier(R.nib.textCell.reuseIdentifier,forIndexPath:indexPath)
letlightFontTitle=UIFont(name:"Acme-Light"ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,size:22)
letlightFontTitle=R.font.acmeLight(size:22)
4
letjsonURL=NSBundle.mainBundle().URLForResource( "seed-data" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,withExtension: "son" letjsonURL=R.file.seedDataJson

和同类型的其他开源库对比的优势

其他同类型的第三方库有:Shark,Natalie,SwiftGen

R.swift的优势有:

  • 通过项目文件(Xcodeproj)来检测资源而不是通过扫描文件里的资源

  • 支持多种资源类型

  • 设计之初接口就希望接近苹果原生API,让你快速上手

支持iOS7.0+

强烈建议项目只支持稳定的iOS8,但是这个库确实支持iOS7

运行原理

每当项目build时,R.swift开始运行。它会侦测工程文件里包含的资源文件,接着生成一个 R.generated.swift的文件。这个文件根据项目里的资源文件按照类型生成结构体。

安装

因为R.swift是在每次项目编译时运行,所以配置和其他第三方库有些区别。这里单独写了一篇介绍Installation:如何安装R.swift

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...