Skip to content

Commit

Permalink
Documentation updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mac-cain13 committed Sep 19, 2024
1 parent 1965acc commit cad6747
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 53 deletions.
39 changes: 39 additions & 0 deletions Documentation/Examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,42 @@ class RecentsController: UICollectionViewController {
}
}
```

## Project

*Vanilla*
```swift
let developmentRegion = fatalError("Not available at runtime")
let myTag = "myTag"
```

*With R.swift*

Access the development region and any asset tags that are set on the project file.

```swift
let developmentRegion = R.project.developmentRegion
let myTag = R.project.knownAssetTags.myTag
```

## Entitlements

*With R.swift*

Access the values in the entitlement file you embedded. This might differ from the entitlements your app actually has at runtime! But it's greate to get some identifiers in a consistent way.

```swift
let appGroupIdentifier = R.entitlements.comAppleSecurityApplicationGroups.groupMyAppGroup
```

## Info.plist

Values under `UIApplicationShortcutItems`, `UIApplicationSceneManifest`, `NSUserActivityTypes`, `NSExtension` that are often needed in code are available directly through R.swift.

*With R.swift*

Access the values in the entitlement file you embedded. This might differ from the entitlements your app actually has at runtime! But it's greate to get some identifiers in a consistent way.

```swift
let sceneConfiguration = UISceneConfiguration(name: R.info.uiApplicationSceneManifest.uiSceneConfigurations.uiWindowSceneSessionRoleApplication.defaultConfiguration.uiSceneConfigurationName, sessionRole: .windowApplication)
```
Binary file added Documentation/Images/RswiftSPMInstallation.mp4
Binary file not shown.
11 changes: 2 additions & 9 deletions Documentation/QandA.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

Swift is a beautiful language and one of it's main advantages is its increasing popularity. However, it can be frustrating to deal with errors that compile but fail during runtime due to missing resources. This makes refactoring difficult while making it easy to create bugs (e.g. missing images etc).

Android tackles this problem by generating something called the R class. It inspired me to create this very project, R.swift, which, thankfully, was well received by colleagues, friends and Github stargazers, so here we are now.``
Android tackles this problem by generating something called the R class. It inspired me to create this very project, R.swift, which, thankfully, was well received by colleagues, friends and Github stargazers, so here we are now.

## Why should I choose R.swift over alternative X or Y?

There are many nice R.swift alternatives like [SwiftGen](https://github.com/AliSoftware/SwiftGen), [Shark](https://github.com/kaandedeoglu/Shark) and [Natalie](https://github.com/krzyzanowskim/Natalie). However, I believe R.swift has these important advantages:
There are many nice R.swift alternatives like [SwiftGen](https://github.com/AliSoftware/SwiftGen) and [Shark](https://github.com/kaandedeoglu/Shark). However, I believe R.swift has these important advantages:
- R.swift inspects your Xcodeproj file for resources instead of scanning folders or asking you for files
- R.swift supports a lot of different assets
- R.swift stays very close to the vanilla Apple API's, having minimal code change with maximum impact
Expand All @@ -17,12 +17,6 @@ There are many nice R.swift alternatives like [SwiftGen](https://github.com/AliS

R.swift works with Xcode 10 for apps targetting iOS 8 and tvOS 9 and higher.

## How do I use methods with a `Void` argument?

Xcode might autocomplete a function with a `Void` argument (`R.image.settingsIcon(Void)`); to solve this, simply remove the `Void` argument and you're good to go: `R.image.settingsIcon()`.

The reason this happens is because of the availability of the var `R.image.settingsIcon.*` for information about the image and also having a function with named the same name.

## How do I fix missing imports in the generated file?

If you get errors like `Use of undeclared type 'SomeType'` in the `R.generated.swift` file, this can usually be fixed by [explicitly stating the module in your xib or storyboard](Images/ExplicitCustomModule.png). This will make R.swift recognize that an import is necessary.
Expand All @@ -45,4 +39,3 @@ During installation you add R.swift as a Build phase to your target, basically t
- Every time you build R.swift will run
- It takes a look at your Xcode project file and inspects all resources linked with the target currently build
- It generates a `R.generated.swift` file that contains a struct with types references to all of your resources

62 changes: 18 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ R.swift currently supports these types of resources:
- [Segues](Documentation/Examples.md#segues)
- [Nibs](Documentation/Examples.md#nibs)
- [Reusable cells](Documentation/Examples.md#reusable-table-view-cells)
- [Project](Documentation/Examples.md#project)
- [Entitlements](Documentation/Examples.md#entitlements)
- [Info.plist](Documentation/Examples.md#info-plist)

Runtime validation with [`R.validate()`](Documentation/Examples.md#runtime-validation):
- If all images used in storyboards and nibs are available
Expand All @@ -74,7 +77,6 @@ Runtime validation with [`R.validate()`](Documentation/Examples.md#runtime-valid
- [Why was R.swift created?](Documentation/QandA.md#why-was-rswift-created)
- [Why should I choose R.swift over alternative X or Y?](Documentation/QandA.md#why-should-i-choose-rswift-over-alternative-x-or-y)
- [What are the requirements to run R.swift?](Documentation/QandA.md#what-are-the-requirements-to-run-rswift)
- [How to use methods with a `Void` argument?](Documentation/QandA.md#how-to-use-methods-with-a-void-argument)
- [How to fix missing imports in the generated file?](Documentation/QandA.md#how-to-fix-missing-imports-in-the-generated-file)
- [How to use classes with the same name as their module?](Documentation/QandA.md#how-to-use-classes-with-the-same-name-as-their-module)
- [Can I ignore resources?](Documentation/Ignoring.md)
Expand All @@ -89,19 +91,24 @@ As of Rswift 7, Swift Package Manager is the recommended method of installation.

[Demo video: Updating from R.swift 6 to Rswift 7](https://youtu.be/icihJ_hin3I?t=66) (Starting at 1:06, this describes the installation of Rswift 7).

### Xcode project - SPM
### Xcode project using SPM (Recommended)

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. Build your project, now the `R` struct should be available in your code, use auto-complete to explore all static references
[Demo Video: Install R.swift in Xcode with SPM](Documentation/RswiftSPMInstallation.mp4)

_Screenshot of the Build Phase can be found [here](Documentation/Images/RunBuildToolPluginsRswift.png)_
1. In Project Settings, on the tab "Package Dependencies", click "+", search for `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".
4. Now click on your target, on the tab "Build Phases", in the section "Run Build Tool Plug-ins", click "+" and add `RswiftGenerateInternalResources`. ([Screenshot](Documentation/Images/RunBuildToolPluginsRswift.png))
5. Now the `R` struct should be available in your code, use auto-complete to explore all static references.

#### When running on Xcode Cloud
Note: The first build you might need to approve the new plugin by clicking the build error warning you about the new plugin.

5. Add a [custom build script](https://developer.apple.com/documentation/xcode/writing-custom-build-scripts) in `ci_scripts/ci_post_clone.sh` with the content:
`defaults write com.apple.dt.Xcode IDESkipPackagePluginFingerprintValidatation -bool YES`
#### 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:

5. 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](https://developer.apple.com/documentation/xcode/writing-custom-build-scripts) in `ci_scripts/ci_post_clone.sh` with this line that Xcode will run.

### Package.swift based SPM project

Expand Down Expand Up @@ -139,36 +146,6 @@ _Screenshot of the Build Phase can be found [here](Documentation/Images/BuildPha
_Tip:_ Add the `*.generated.swift` pattern to your `.gitignore` file to prevent unnecessary conflicts.
</details>


<details>
<summary><h3>Mint</h3></summary>

0. Add the [R.swift](https://github.com/mac-cain13/R.swift) library to your project
1. Add `mac-cain13/R.swift` to your [Mintfile](https://github.com/yonaskolb/Mint#mintfile) and run `mint bootstrap` to install this package without linking it globally (recommended)
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, expand it and paste the following script:
```bash
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
```
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`

_Tip:_ Add the `*.generated.swift` pattern to your `.gitignore` file to prevent unnecessary conflicts.
</details>


<details>
<summary><h3>Homebrew</h3></summary>

R.swift is also available through [Homebrew](http://brew.sh). 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](https://github.com/tomasharkema).
</details>

<details>
<summary><h3>Manually</h3></summary>

Expand All @@ -188,13 +165,10 @@ _Screenshot of the Build Phase can be found [here](Documentation/Images/ManualBu
_Tip:_ Add the `*.generated.swift` pattern to your `.gitignore` file to prevent unnecessary conflicts.
</details>


## Contribute

We'll love contributions, read the [contribute docs](Documentation/Contribute.md) for info on how to report issues, submit ideas and submit pull requests!

## License

[R.swift](https://github.com/mac-cain13/R.swift) and [R.swift.Library](https://github.com/mac-cain13/R.swift.Library) are created by [Mathijs Kadijk](https://github.com/mac-cain13) and released under a [MIT License](License).

Special thanks to [Tom Lokhorst](https://github.com/tomlokhorst) for his major contributions and help maintaining this project.
[R.swift](https://github.com/mac-cain13/R.swift) is created by [Mathijs Kadijk](https://github.com/mac-cain13) and [Tom Lokhorst](https://github.com/tomlokhorst) released under a [MIT License](License).

0 comments on commit cad6747

Please sign in to comment.