99热99这里只有精品6国产,亚洲中文字幕在线天天更新,在线观看亚洲精品国产福利片 ,久久久久综合网

歡迎加入QQ討論群258996829
贏了現(xiàn)在輸了未來 頭像
蘋果2袋
2
贏了現(xiàn)在輸了未來

R.swift:以一種優(yōu)雅安全的方式使用資源文件

發(fā)布時間:2016-01-25 20:58  回復(fù):0  查看:2768   最后回復(fù):2016-01-25 20:58  

引言:當前使用資源文件存在的問題

先來看下目前如果我們要使用資源文件時代碼是如何調(diào)用的:

let icon = UIImage(named: "settings-icon") 
let font = UIFont(name: "San Francisco", size: 42) 
performSegueWithIdentifier("openSettings")
這種通過傳入字符串來獲取資源有很大的潛在的風(fēng)險:
  • 最常見的就是資源名稱拼寫錯誤。如果項目資源很多檢查拼寫正確也是頗費時間
  • 如果刪除了一個資源文件,只能通過全局搜索資源名稱來判斷是否已經(jīng)沒有使用這個資源

R.swift的解決方案

先看下上面的邏輯用R.swift代碼調(diào)用:

let icon = R.image.settingsIcon 
let font = R.font.sanFrancisco(size:42)
performSegueWithIdentifier(R.segue.openSettings)
R如何解決上面的問題:
  • 強類型
    使用一個資源前,先聲明是什么類型。如果是一個圖片資源就是R.image.xx。這樣每次明確知道使用的資源類型。(swift是一門強類型語言,強類型的一個好處就是很多錯誤可以在編譯時就發(fā)現(xiàn))
  • 編譯時檢查資源文件是否存在
  • 自動填充資源名稱
    因為會自動根據(jù)資源文件生成結(jié)構(gòu)體,所以可以直接使用,不用自己拼寫資源名

支持的資源類型

//使用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
//使用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()
//使用R.swift之前
performSegueWithIdentifier("openSettings")
//使用R.swift
performSegueWithIdentifier(R.segue.openSettings)
//使用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)
//使用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)
//使用R.swift之前
let lightFontTitle = UIFont(name: "Acme-Light", size: 22)
//使用R.swift
let lightFontTitle = R.font.acmeLight(size: 22)
//使用R.swift之前
let jsonURL = NSBundle.mainBundle().URLForResource("seed-data", withExtension: "son")
//使用R.swift
let jsonURL = R.file.seedDataJson

和同類型的其他開源庫對比的優(yōu)勢

其他同類型的第三方庫有: SharkNatalie , SwiftGen
R.swift的優(yōu)勢有:

  • 通過項目文件(Xcodeproj)來檢測資源而不是通過掃描文件里的資源
  • 支持多種資源類型
  • 設(shè)計之初接口就希望接近蘋果原生API,讓你快速上手

支持iOS7.0+

強烈建議項目只支持穩(wěn)定的iOS8,但是這個庫確實支持iOS7

運行原理

每當項目build時,R.swift開始運行。它會偵測工程文件里包含的資源文件,接著生成一個 R.generated.swift的文件。這個文件根據(jù)項目里的資源文件按照類型生成結(jié)構(gòu)體。

安裝

因為R.swift是在每次項目編譯時運行,所以配置和其他第三方庫有些區(qū)別。這里單獨寫了一篇介紹Installation:如何安裝R.swift


轉(zhuǎn)自http://www.jianshu.com/p/b453b78c7126

您還未登錄,請先登錄

熱門帖子

最新帖子

?