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



引言:当前使用资源文件存在的问题 先来看下目前如果我们要使用资源文件时代码是如何调用的:

let icon = UIImage(named: "settings-icon") 
let font = UIFont(name: "San Francisco", size: 42) 
performSegueWithIdentifier("openSettings")

这种通过传入字符串来获取资源有很大的潜在的风险: 最常见的就是资源名称拼写错误。如果项目资源很多检查拼写正确也是颇费时间 如果删除了一个资源文件,只能通过全局搜索资源名称来判断是否已经没有使用这个资源 R.swift的解决方案 先看下上面的逻辑用R.swift代码调用:

let icon = R.image.settingsIcon 
let font = R.font.sanFrancisco(size:42)
performSegueWithIdentifier(R.segue.openSettings)

R如何解决上面的问题: 强类型 使用一个资源前,先声明是什么类型。如果是一个图片资源就是R.image.xx。这样每次明确知道使用的资源类型。(swift是一门强类型语言,强类型的一个好处就是很多错误可以在编译时就发现) 编译时检查资源文件是否存在 自动填充资源名称 因为会自动根据资源文件生成结构体,所以可以直接使用,不用自己拼写资源名 支持的资源类型 Images

//使用R.swift之前
let settingsIcon = UIImage(named: "settings-icon")
let gradientBackground = UIImage(named: "gradient.jpg")
//使用R.swift
let settingsIcon = R.image.settingsIcon
let gradientBackground = R.image.gradientJpg

Storyboards

//Storyboards使用R.swift之前
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialTabBarController = storyboard.instantiateInitialViewController() as? UITabBarController
let settingsController = self.instantiateViewControllerWithIdentifier("settingsController") as? SettingsController
//使用R.swift
let storyboard = R.storyboard.main.instance
let initialTabBarController = R.storyboard.main.initialViewController
let settingsController = R.storyboard.main.settingsController
//通过这个代码来校验运行时storyboard的图片是否都能被加载
// 只在debug模式下有效,会通过断言来提示
R.storyboard.main.validateImages()
//在运行时校验所有的viewController能够被正常加载
mode.R.storyboard.main.validateViewControllers()

Segues

//使用R.swift之前
performSegueWithIdentifier("openSettings")
//使用R.swift
performSegueWithIdentifier(R.segue.openSettings)

Nibs

//使用R.swift之前
let nameOfNib = "CustomView"
let customViewNib = UINib(nibName: "CustomView", bundle: nil)
let rootViews = customViewNib.instantiateWithOwner(nil, options: nil)
let customView = rootViews[0] as? CustomView
let viewControllerWithNib = CustomViewController(nibName: "CustomView", bundle: nil)
//使用R.swift
let nameOfNib = R.nib.customView.name
let customViewNib = R.nib.customView
let rootViews = R.nib.customView.instantiateWithOwner(nil, options: nil)
let customView = R.nib.customView.firstView(nil, options: nil)
let viewControllerWithNib = CustomViewController(nib: R.nib.customView)

Reusable cells

//使用R.swift之前
let textCellNib = UINib(nibName: "TextCell", bundle: nil) 
tableView.registerNib(textCellNib, forCellReuseIdentifier: "TextCellIdentifier")
//使用R.swift
tableView.registerNib(R.nib.textCell)
//cellForRowAtIndexPath中获取cell
let textCell = tableView.dequeueReusableCellWithIdentifier(R.nib.textCell.reuseIdentifier, forIndexPath: indexPath)

Custom fonts

//使用R.swift之前
let lightFontTitle = UIFont(name: "Acme-Light", size: 22)
//使用R.swift
let lightFontTitle = R.font.acmeLight(size: 22)

Resource files

//使用R.swift之前
let jsonURL = NSBundle.mainBundle().URLForResource("seed-data", withExtension: "son")
//使用R.swift
let jsonURL = R.file.seedDataJson

和同类型的其他开源库对比的优势 
其他同类型的第三方库有: Shark, Natalie , SwiftGen 

R.swift的优势有: 
通过项目文件(Xcodeproj)来检测资源而不是通过扫描文件里的资源 支持多种资源类型 设计之初接口就希望接近苹果原生API,让你快速上手 支持iOS7.0+ 
强烈建议项目只支持稳定的iOS8,但是这个库确实支持iOS7 

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


0