Cette page est affichée en anglais. Une traduction française est en cours.
ScriptsJul 6, 2026·3 min de lecture

R.swift — Strong Typed Resource Access for Swift

A code generator that provides autocompleted, compile-time checked access to images, fonts, storyboards, and other resources in Swift projects.

Prêt pour agents

Installation agent prête

Cet actif peut être installé après choix du runtime, vérification du plan et exécution de la commande adaptée.

Native · 98/100Policy : autoriser
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
R.swift Overview
Commande d'installation directe
npx -y tokrepo@latest install bf811983-7959-11f1-9bc6-00163e2b0d79 --target codex

À exécuter après confirmation du plan en dry-run.

Introduction

R.swift generates a Swift file that gives you strong typed, autocompleted references to your project resources. Typos in image names, missing storyboard identifiers, or incorrect font names become compile-time errors instead of runtime crashes. It follows the same concept as Android's R class.

What R.swift Does

  • Generates typed accessors for images, colors, fonts, nibs, storyboards, and segues
  • Catches resource reference errors at compile time instead of runtime
  • Provides full Xcode autocompletion for all resource names
  • Runs as a build plugin that regenerates on every build
  • Supports asset catalogs, localized strings, and custom file types

Architecture Overview

R.swift runs as an SPM build tool plugin during the Xcode build phase. It scans your project for resource files (asset catalogs, storyboards, XIBs, fonts, localized strings), then generates a single R.generated.swift file containing nested structs with static properties and functions. Each resource type maps to its appropriate UIKit/AppKit initializer, returning non-optional values where the resource is guaranteed to exist.

Self-Hosting & Configuration

  • Add via SPM and enable the R.swift build plugin for your target
  • Requires Xcode 15+ and Swift 5.9+ for build plugin support
  • Compatible with iOS 13+, macOS 10.15+, tvOS 13+, watchOS 6+
  • Customize generation with an rswift.toml config file to exclude certain resource types
  • For legacy setups, a standalone CLI binary is also available

Key Features

  • Zero-overhead at runtime; all resource references resolve to standard initializer calls
  • Supports images, colors, fonts, nibs, storyboards, segues, reuse identifiers, and localized strings
  • Validates that storyboard identifiers and segue names exist at compile time
  • Works alongside SwiftGen without conflicts for teams migrating gradually
  • Output is a plain Swift file that can be committed for environments without the plugin

Comparison with Similar Tools

  • SwiftGen — template-based code generator with more customizable output formats; R.swift is simpler to set up
  • Asset Catalog Compiler (actool) — built into Xcode; only handles images and colors, not storyboards or fonts
  • String literal references — zero setup but no compile-time safety or autocompletion
  • Tuist resource accessors — integrated into Tuist project generation; requires Tuist as your build system
  • Sourcery — general-purpose code generator; can do resource typing but requires custom templates

FAQ

Q: Does R.swift work with SwiftUI? A: Yes. The generated accessors return standard types (UIImage, Color, Font) that work in both UIKit and SwiftUI contexts.

Q: How does R.swift handle localized strings? A: It generates typed functions for each Localizable.strings key with parameter placeholders, so format arguments are type-checked.

Q: Will it slow down my build? A: The generation step adds a few seconds to the build. It runs incrementally and only regenerates when resource files change.

Q: Can I use R.swift with Objective-C? A: The generated file is Swift-only. For mixed projects, use the generated Swift types from Swift code and keep string-based references in Objective-C files.

Sources

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires