Zurück zum Ranking

mac-cain13/R.swift

Swift

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

swiftioswatchostvoscode-generatorautocompletionxcoderesources
Sterne-Wachstum
Sterne
9.6k
Forks
779
Wochenwachstum
Issues
72
5k
Jan. 2015Nov. 2018Sept. 2022Juli 2026
README

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

Demo Video: Install R.swift in Xcode with SPM

  1. In Project Settings, on the tab "Package Dependencies", click "+", search for https://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 add RswiftGenerateInternalResources. (Screenshot)
  4. Now the R 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 a custom build script in ci_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 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.

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 is created by Mathijs Kadijk and Tom Lokhorst released under a MIT License.

Ähnliche Repositories
justjavac/free-programming-books-zh_CN

:books: 免费的计算机编程类中文书籍,欢迎投稿

GNU General Public License v3.0pythonjavascript
weibo.com/justjavac
117.7k28.2k
appwrite/appwrite

Appwrite® - complete cloud infrastructure for your web, mobile and AI apps. Including Auth, Databases, Storage, Functions, Messaging, Hosting, Realtime and more

TypeScriptnpmBSD 3-Clause "New" or "Revised" Licenseappwritedocker
appwrite.io
56.6k5.6k
dkhamsing/open-source-ios-apps

:iphone: Collaborative List of Open-Source iOS Apps

Creative Commons Zero v1.0 Universaliosswift
51.3k6k
serhii-londar/open-source-mac-os-apps

🚀 Awesome list of open source applications for macOS. https://t.me/s/opensourcemacosapps

Creative Commons Zero v1.0 Universalmacosmac
49.7k2.6k
iina/iina

The modern video player for macOS.

SwiftGNU General Public License v3.0macosvideo-player
iina.io
45.7k2.9k
Alamofire/Alamofire

Elegant HTTP Networking in Swift

SwiftMIT Licensenetworkingurlsession
42.4k7.7k
xitu/gold-miner

🥇掘金翻译计划,可能是世界最大最好的英译中技术社区,最懂读者和译者的翻译平台:

androidtranslation
juejin.cn/tag/%E6%8E%98%E9%87%91%E7%BF%BB%E8%AF%91%E8%AE%A1%E5%88%92
34.3k5k
MonitorControl/MonitorControl

🖥 Control your display's brightness & volume on your Mac as if it was a native Apple Display. Use Apple Keyboard keys or custom shortcuts. Shows the native macOS OSDs.

SwiftMIT Licensemacoskeyboard
monitorcontrol.app
33.8k982
shadowsocks/ShadowsocksX-NG

Next Generation of ShadowsocksX

SwiftGNU General Public License v3.0shadowsocksswift
32.9k7.8k
kodecocodes/swift-algorithm-club

Algorithms and data structures in Swift, with explanations!

SwiftMIT Licenseswiftdata-structures
29.1k5k
jordanbaird/Ice

Powerful menu bar manager for macOS

SwiftGNU General Public License v3.0macosmenubar
icemenubar.app
29k821
airbnb/lottie-ios

An iOS library to natively render After Effects vector animations

SwiftApache License 2.0animationbodymovin
lottie.airbnb.tech
26.8k3.8k