Tool that generates a swift constants file from resource files names.
What you do:
guard let url = Bundle.main.url(forResource: "ErrorProneResourceFileNameWhichCanBeChangedWithouyNoticing",
withExtension: nil) else {
return
}
What you want to do:
guard let url = Bundle.main.url(forResource: Files.fileName,
withExtension: nil) else {
return
}
- Get safety for free.
- Autocomplete.
- Build errors when constants change.
- Navigate to the desired directory in the terminal (eg. in the project's
bin/
folder) - run
curl -O https://raw.githubusercontent.com/andreinagy/SwiftConstGen/master/swiftFilesConstGen.rb
- run
chmod +x swiftFilesConstGen.rb
- Profit
Optionally you can add it as a build phase to get a fresh constants file.
"$PROJECT_DIR/bin/swiftFilesConstGen.rb" -i "$SRCROOT/$PROJECT_NAME/Resources/" -o "$SRCROOT/$PROJECT_NAME/Generated/" -e json -n JSONFiles
- Script location: project's
bin
directory - Input files location
Project/Resources/
- Input files extension
json
- Output location
Project/Generated/
- Output file
JSONFiles-generated.swift
Run in the terminal:
./swiftFilesConstGen -i path/to/files -o output/path -e json -n JSONFiles
- It searches for all files ending with
json
inpath/to/files
- Creates
JSONFiles-generated.swift
containing constants with the files names. - Handles replaces the spaces and hyphens with underscores. Files that start with a number get an
a
prepended.
// Generated by swiftFilesConstGen
// Input directory: TestInput/
// Files extension: json
// Struct name: JSONFiles
// Do not modify manually
// swiftlint:disable all
struct JSONFiles {
static let fileExtension = "json"
static let myOtherFile = "My--Other.File"
static let myFile = "My-File"
}
// swiftlint:enable all