R.swift for Swift: Complete Guide with Examples
\n\n1. What It Is and Why You Need It
\n\nR.swift is a powerful library for the Swift language that provides type-safe access to resources of your iOS/macOS/tvOS/watchOS application. Instead of using string literals to reference images, fonts, colors, localized strings, and other resources, R.swift generates strictly typed structures for them. This means the compiler can verify the correctness of a resource name at compile time, rather than during application runtime.\n
\n\nWhy is this needed? In standard Apple development, you often write code like UIImage(named: "myImage") or NSLocalizedString("hello_key", comment: ""). If you make a typo in a file name or localization key, the application will crash with an error only at runtime. R.swift solves this problem radically: after code generation, you access resources as properties of generated structures. For example: R.image.myImage or R.string.localizable.helloKey(). The compiler will immediately point to a non-existent resource, and Xcode will offer autocompletion.\n
\nAdditionally, R.swift automatically handles Asset Catalogs, .strings files, .storyboard, .xib, .json, .plist, fonts, colors, and even Data files. This makes code cleaner, safer, and significantly speeds up development thanks to autocompletion in Xcode. The library is actively maintained by the community and is used in thousands of projects worldwide.\n
\n2. Installation
\n\nR.swift is installed via Swift Package Manager (SPM) or CocoaPods. We recommend SPM as it is the modern standard for Swift projects.\n
\n\nInstallation via Swift Package Manager (SPM)
\nAdd the package to your project via Xcode:
\n- \n
- Open your project in Xcode. \n
- Go to File > Add Packages.... \n
- Paste the URL:
https://github.com/mac-cain13/R.swift\n - Select the version (latest stable recommended) and click Add Package. \n
- Ensure the package is added to your main target. \n
You can also add it manually in Package.swift:
// swift-tools-version:5.7\nimport PackageDescription\n\nlet package = Package(\n name: "YourProject",\n dependencies: [\n .package(url: "https://github.com/mac-cain13/R.swift", from: "7.0.0")\n ],\n targets: [\n .target(\n name: "YourTarget",\n dependencies: [\n .product(name: "RswiftLibrary", package: "R.swift")\n ]\n )\n ]\n)\n\n\nInstallation via CocoaPods
\nIf you use CocoaPods, add to Podfile:
platform :ios, '13.0'\nuse_frameworks!\n\ntarget 'YourApp' do\n pod 'R.swift', '~> 7.0'\nend\n\nThen run the command:
\npod install\n\n\nBuild Phase Setup (mandatory!)
\nAfter installation, you need to add a Run Script Phase in the Build Phases of your target:
\n- \n
- Go to Build Phases of your target. \n
- Click + and select New Run Script Phase. \n
- Drag this phase so it runs before the Compile Sources phase. \n
- In the script field, paste: \n
"$PODS_ROOT/R.swift/rswift" generate "$SRCROOT/R.generated.swift"\n\nIf you use SPM, the script will look like this:
\n"${BUILD_DIR%/Build/*}/SourcePackages/checkouts/R.swift/rswift" generate "$SRCROOT/R.generated.swift"\n\nAfter building the project, a file will be generated Also in library