zoukankan      html  css  js  c++  java
  • R.Swift优雅加载资源文件

    在新的项目中,接触到了一个很不错的框架R.swift,可以帮助更方便安全的使用资源文件,相信已经使用过的或者还没有接触过的,一旦使用过了解过它,会爱上这个框架工具!

    一、R.swift特点

    • 当项目build之后,R.swift开始运行,也就是说添加完图片等资源文件时,build一下,R.swift第三方库就会设置好刚刚添加的资源.
    • 加入的资源文件在build后自动在R.generated.swift文件中按照类型生成结构体.
    • 强类型,不需要类型判断和转换,自动返回对应类型.
    • 支持了多种资源类型.
    • 避免了资源名称拼写错误.

    二、安装

    1. 添加pod 'R.swift'到你的Podfile文件中,紧接着运行pod install
    2. 打开工程文件,点击工程文件名称,选择TARGETS,点击Build Phases,在点击左上角的“+”添加New Run Script Phas

      3. 脚本输入"$PODS_ROOT/R.swift/rswift" generate "$SRCROOT/R.generated.swift",input files 添加   $TEMP_DIR/rswift-lastrun,out files 添加 $SRCROOT/R.generated.swift

      4. 拖拽R.generated.swift文件到项目中.

    三、具体使用

    1. 图片-images

    原生写法

    let sIcon = UIImage(named: "settings-icon")

    使用R.swift

    func icon() -> UIImage? {
            switch self {
            case .sourceRegulator:
                return R.image.home_SourceRegulatory()
            case .regulation:
                return R.image.home_regulationIcon()
            case .broker:
                return R.image.home_brokerIcon()
            case .engine:
                return R.image.home_engineIcon()
            case .falseBroker:
                return R.image.home_falseBrokerIcon()
            case .spread:
                return R.image.home_spredIcon()
            }
        }

    2. 文件-Files

    原始写法

    let plistURL = Bundle.main.url(forResource: "Book", withExtension: "plist")
    let jsonPath = Bundle.main.path(forResource: "data", ofType: "json")

    使用R.swift后

    let plistURL = R.file.bookPlist()
    let jsonPath = R.file.DataJson.path()

    3.字体-Fonts

    原始用法

    let lightFontTitle = UIFont(name: "chalkduster", size: 22)

    使用R.swift

    R.font.chalkduster(size: 35)

    4.Localized strings

    原始写法

    let welcomeMessage = NSLocalizedString("welcome.message", comment: "")
    let settingsTitle = NSLocalizedString("title", tableName: "Settings", comment: "")
    
    // Formatted strings
    let welcomeName = String(format: NSLocalizedString("welcome.withName", comment: ""), locale: NSLocale.current, "Alice")
    
    // Stringsdict files
    let progress = String(format: NSLocalizedString("copy.progress", comment: ""), locale: NSLocale.current, 4, 23)

    使用R.swift

    // Localized strings are grouped per table (.strings file)
    let welcomeMessage = R.string.localizable.welcomeMessage()
    let settingsTitle = R.string.settings.title()
    
    // Functions with parameters are generated for format strings
    let welcomeName = R.string.localizable.welcomeWithName("Alice")
    
    // Functions with named argument labels are generated for stringsdict keys
    let progress = R.string.localizable.copyProgress(completed: 4, total: 23)

    上面就是本人项目经常使用到的,当然还有其他的用法和用处,不过,通过R.swift已经大大的方便我们日常开发,希望大家在项目中尽早的使用R.swift这个第三方库.

  • 相关阅读:
    AppCan学习笔记----Request和登录功能简单实现
    漫谈:从APP崩溃率标准,到Monkey介绍拓展Maxim,及Jenkins自动化配置,持续集成获取崩溃monkey日志
    Android:adb shell 命令详解
    Android:adb命令详解
    常用获取Android崩溃日志和IOS崩溃日志的几种方法
    numpy 介绍和基础使用详解
    软件测试之路再谈(三年测试风雨)
    HTTP协议介绍
    Android Jenkins自动打包纪录
    Android专项测试监控资源
  • 原文地址:https://www.cnblogs.com/guohai-stronger/p/11791211.html
Copyright © 2011-2022 走看看