美文网首页
一、R.swift以一种优雅的方式管理资源文件

一、R.swift以一种优雅的方式管理资源文件

作者: ISwiftUI | 来源:发表于2017-02-27 09:12 被阅读342次

    Github源码地址

    https://github.com/mac-cain13/R.swift

    安装

    1.CocoaPods安装
    在run script中贴进下面的代码:
    "$PODS_ROOT/R.swift/rswift" "$SRCROOT"
    build完成后,会在项目的根目录下发现R.generated.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
    //使用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
    

    相关文章

      网友评论

          本文标题:一、R.swift以一种优雅的方式管理资源文件

          本文链接:https://www.haomeiwen.com/subject/vzzawttx.html