Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

mac-cain13/R.swift

Repository files navigation

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:

leticon=UIImage(named:"settings-icon")letfont=UIFont(name:"San Francisco", size:42)letcolor=UIColor(named:"indicator highlight")letviewController=CustomViewController(nibName:"CustomView", bundle:nil)letstring=String(format:NSLocalizedString("welcome.withName", comment:""), locale:NSLocale.current,"Arthur Dent")

With R.swift it becomes:

leticon=R.image.settingsIcon()letfont=R.font.sanFrancisco(size:42)letcolor=R.color.indicatorHighlight()letviewController=CustomViewController(nib:R.nib.customView)letstring=R.string.localizable.welcomeWithName("Arthur Dent")

Check outmore examples or hear abouthow Fabric.app uses R.swift!

Demo

Autocompleted images:

Autocompleted images

Compiletime checked images:

Compiletime checked images

This is only the beginning, check outmore 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 theR-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 withR.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 using SPM (Recommended)

Demo Video: Install R.swift in Xcode with SPM

  1. In Project Settings, on the tab "Package Dependencies", click "+", search forhttps://github.com/mac-cain13/R.swift and click "Add Package".
  2. Select the target that will use R.swift next to "RswiftLibrary" and click "Add Package".
  3. Now click on your target, on the tab "Build Phases", in the section "Run Build Tool Plug-ins", click "+" and addRswiftGenerateInternalResources. (Screenshot)
  4. Now theR struct should be available in your code, use auto-complete to explore all static references.

Note: The first build you might need to approve the new plugin by clicking the build error warning you about the new plugin.

R.swift on Xcode Cloud or any other CI

On your CI server you can't explicitly allow the build plugin to run, so you need to disable plugin validation to be able to build without user interaction:

  1. Run a script on your CI that runs:defaults write com.apple.dt.Xcode IDESkipPackagePluginFingerprintValidatation -bool YES before Xcode starts building.

On Xcode Cloud you can add acustom build script inci_scripts/ci_post_clone.sh with this line that Xcode will run.

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 theR struct should be available in your code, use auto-complete to explore all static references

CocoaPods

  1. Addpod 'R.swift' to yourPodfile and runpod install
  2. In Xcode: Click on your project in the file list, choose your target underTARGETS, click theBuild Phases tab and add aNew Run Script Phase by clicking the little plus icon in the top left
  3. Drag the newRun Script phaseabove theCompile Sources phase andbelowCheck 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 aR.generated.swift in the$SRCROOT-folder, drag theR.generated.swift files into your project anduncheckCopy items if needed

Screenshot of the Build Phase can be foundhere

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

Manually

  1. Add theR.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 underTARGETS, click theBuild Phases tab and add aNew Run Script Phase by clicking the little plus icon in the top left
  4. Drag the newRun Script phaseabove theCompile 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 aR.generated.swift in the$SRCROOT-folder, drag theR.generated.swift files into your project anduncheckCopy items if needed

Screenshot of the Build Phase can be foundhere

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

Contribute

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

License

R.swift is created byMathijs Kadijk andTom Lokhorst released under aMIT License.

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp