R.Swift elegant load the resource file

In the new project, access to a very good framework R.swift, can help more convenient and safe use of the resource file, believed to have been used or have not been in contact, once used to find out about it, you will love this frame tool!

A, R.swift Features

  • When the project build, R.swift running, that is done adding pictures and other resource files, build it, R.swift third-party libraries will be set up resource you just added.
  • Was added according to the type of resource files automatically generated file structure in R.generated.swift after build.
  • Strong type, and does not require the type of conversion is determined automatically returns the corresponding type.
  • It supports a variety of resource types.
  • Avoiding the resource name misspelled.

 

Second, the installation

  1. Add pod 'R.swift' to your Podfile file, followed by Run pod install
  2. Open the project file, click the project file name, select TARGETS, click Build Phases, click in the upper left corner of the "+" Add New Run Script Phas

  3. Open and copy the code into the following: "$ PODS_ROOT / R.swift / rswift" generate "$ SRCROOT / R.generated.swift"

  4. Drag R.generated.swift file to the project.

 

Third, the specific use

1. Picture -images

Native wording

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

Use 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. File -Files

Original wording

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

After using R.swift

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

 

3. Fonts -Fonts

Original usage

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

Use R.swift

R.font.chalkduster(size: 35)

 

4.Localized strings

Original wording

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)

Use 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)

 

Above is often used to project himself, of course, there are other uses and usefulness, however, by R.swift has greatly facilitate our daily development, we hope to use this R.swift third-party libraries as early as possible in the project.

Guess you like

Origin www.cnblogs.com/guohai-stronger/p/11791211.html