GithubHelp home page GithubHelp logo

jessearmandse / r.swift Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mac-cain13/r.swift

0.0 0.0 0.0 4.43 MB

Strong typed, autocompleted resources like images, fonts and segues in Swift projects

License: MIT License

Ruby 0.54% Swift 99.46%

r.swift's Introduction

R.swift Version License Platform

Get strong typed, autocompleted resources like images, fonts and segues in Swift projects

Why use this?

It makes your code that uses resources:

  • Fully typed, less casting and guessing what a method will return
  • Compile time checked, no more incorrect strings that make your app crash at runtime
  • Autocompleted, never have to guess that image name again

Currently you type:

let icon = UIImage(named: "settings-icon")
let font = UIFont(name: "San Francisco", size: 42)
let color = UIColor(named: "indicator highlight")
let viewController = CustomViewController(nibName: "CustomView", bundle: nil)
let string = String(format: NSLocalizedString("welcome.withName", comment: ""), locale: NSLocale.current, "Arthur Dent")

With R.swift it becomes:

let icon = R.image.settingsIcon()
let font = R.font.sanFrancisco(size: 42)
let color = R.color.indicatorHighlight()
let viewController = CustomViewController(nib: R.nib.customView)
let string = R.string.localizable.welcomeWithName("Arthur Dent")

Check out more examples or hear about how Fabric.app uses R.swift!

Demo

Autocompleted images:

Autocompleted images

Compiletime checked images:

Compiletime checked images

This is only the beginning, check out more examples!

CocoaHeadsNL presentation

Mathijs Kadijk presented R.swift at the September 2016 CocoaHeadsNL meetup. Talking about the ideas behind R.swift and demonstrating how to move from plain stringly-typed iOS code to statically typed code.

R.swift presentation at CocoaHeadsNL

Features

After installing R.swift into your project you can use the R-struct to access resources. If the struct is outdated just build and R.swift will correct any missing/changed/added resources.

R.swift currently supports these types of resources:

Runtime validation with R.validate():

  • If all images used in storyboards and nibs are available
  • If all named colors used in storyboards and nibs are available
  • If all view controllers with storyboard identifiers can be loaded
  • If all custom fonts can be loaded

Q&A

Installation

As of Rswift 7, Swift Package Manager is the recommended method of installation.

Demo video: Updating from R.swift 6 to Rswift 7 (Starting at 1:06, this describes the installation of Rswift 7).

Xcode project - SPM

  1. In Project Settings, on the tab "Package Dependencies", click "+" and add github.com/mac-cain13/R.swift
  2. Select your target, on the tab "General", in the section "Frameworks, Libraries, and Embeded Content", click "+" and add RswiftLibrary
  3. Select your target, on the tab "Build Phases", in the section "Run Build Tool Plug-ins", click "+" and add RswiftGenerateInternalResources
  4. Right-click on your project, click RswiftModifyXcodePackages, click Run, click Allow Command to Change Files
  5. Build your project, now the R struct should be available in your code, use auto-complete to explore all static references

Screenshot of the Build Phase can be found here

Package.swift based SPM project

  1. Add a dependency in Package.swift:
    dependencies: [
        .package(url: "https://github.com/mac-cain13/R.swift.git", from: "7.0.0")
    ]
  2. For each relevant target, add a dependency and a plugin
    .target(
        name: "Example",
        dependencies: [.product(name: "RswiftLibrary", package: "R.swift")],
        plugins: [.plugin(name: "RswiftGeneratePublicResources", package: "R.swift")]
    )
  3. Build your project, now the R struct should be available in your code, use auto-complete to explore all static references

CocoaPods

  1. Add pod 'R.swift' to your Podfile and run pod install
  2. In Xcode: Click on your project in the file list, choose your target under TARGETS, click the Build Phases tab and add a New Run Script Phase by clicking the little plus icon in the top left
  3. Drag the new Run Script phase above the Compile Sources phase and below Check Pods Manifest.lock, expand it and paste the following script:
    "$PODS_ROOT/R.swift/rswift" generate "$SRCROOT/R.generated.swift"
  4. Add $SRCROOT/R.generated.swift to the "Output Files" of the Build Phase
  5. Uncheck "Based on dependency analysis" so that R.swift is run on each build
  6. Build your project, in Finder you will now see a R.generated.swift in the $SRCROOT-folder, drag the R.generated.swift files into your project and uncheck Copy items if needed

Screenshot of the Build Phase can be found here

Tip: Add the *.generated.swift pattern to your .gitignore file to prevent unnecessary conflicts.

Mint

  1. Add the R.swift library to your project
  2. Add mac-cain13/R.swift to your Mintfile and run mint bootstrap to install this package without linking it globally (recommended)
  3. In Xcode: Click on your project in the file list, choose your target under TARGETS, click the Build Phases tab and add a New Run Script Phase by clicking the little plus icon in the top left
  4. Drag the new Run Script phase above the Compile Sources phase, expand it and paste the following script:
    if mint list | grep -q 'R.swift'; then
      mint run [email protected] rswift generate "$SRCROOT/R.generated.swift"
    else
      echo "error: R.swift not installed; run 'mint bootstrap' to install"
      return -1
    fi
  5. Add $SRCROOT/R.generated.swift to the "Output Files" of the Build Phase
  6. Uncheck "Based on dependency analysis" so that R.swift is run on each build
  7. Build your project, in Finder you will now see a R.generated.swift in the $SRCROOT-folder, drag the R.generated.swift files into your project and uncheck Copy items if needed

Tip: Add the *.generated.swift pattern to your .gitignore file to prevent unnecessary conflicts.

Homebrew

R.swift is also available through Homebrew. This makes it possible to install R.swift globally on your system. Install R.swift by running: brew install rswift. The Homebrew formula is maintained by @tomasharkema.

Manually

  1. Add the R.swift library to your project
  2. Download a R.swift release, unzip it and put it into your source root directory
  3. In Xcode: Click on your project in the file list, choose your target under TARGETS, click the Build Phases tab and add a New Run Script Phase by clicking the little plus icon in the top left
  4. Drag the new Run Script phase above the Compile Sources phase, expand it and paste the following script:
    "$SRCROOT/rswift" generate "$SRCROOT/R.generated.swift"
  5. Add $SRCROOT/R.generated.swift to the "Output Files" of the Build Phase
  6. Uncheck "Based on dependency analysis" so that R.swift is run on each build
  7. Build your project, in Finder you will now see a R.generated.swift in the $SRCROOT-folder, drag the R.generated.swift files into your project and uncheck Copy items if needed

Screenshot of the Build Phase can be found here

Tip: Add the *.generated.swift pattern to your .gitignore file to prevent unnecessary conflicts.

Contribute

We'll love contributions, read the contribute docs for info on how to report issues, submit ideas and submit pull requests!

License

R.swift and R.swift.Library are created by Mathijs Kadijk and released under a MIT License.

Special thanks to Tom Lokhorst for his major contributions and help maintaining this project.

r.swift's People

Contributors

mac-cain13 avatar tomlokhorst avatar shiraji avatar chillpop avatar maciejpiotrowski89 avatar bclymer avatar rafaelnobrepd avatar tomasharkema avatar mbernson avatar lammertw avatar craigsoveritall avatar amichnia avatar uhooi avatar uny avatar eeyore741 avatar robfeldmann avatar pbernery avatar iv-mexx avatar ky1ejs avatar tvede-dk avatar ykws avatar ntnmrndn avatar 9race avatar 417-72ki avatar mfcollins3 avatar nicoelayda avatar uypanha avatar pedrovieira avatar roelspruit avatar imryan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.