Skip to content

indragiek/swiftrsrc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

swiftrsrc

Resource code generation tool for Swift

swiftrsrc generates Swift code for accessing elements of asset catalogs, storyboards, and color lists in order to avoid the error-prone practice of hardcoding strings into your code. It is heavily inspired by Square's objc-codegenutils, which you should definitely look into if you're working on an Objective-C project.

Installing

The simplest way to install swiftrsrc is to download the latest binary from the Releases page.

Usage

swiftrsrc generate --platform [osx|ios] input_path output_path

--platform [ios|osx]
platform to generate code for. Must be either "ios" or "osx"

input_path
input path to generate code from. Must be an *.xcassets, *.storyboard, or *.clr path

output_path
output path to write the generated code to. If a directory path is specified, the generated code will be placed in a Swift source code file with the same name as the struct

Asset Catalogs

The generated code for asset catalogs only includes image sets, and purposely omits app icons and launch images (as these are not typically referred to programatically). If you put image sets inside folders, a corresponding nested struct will be created for the folder. In the example below, Posts and Main are folders inside Images.xcassets:

struct ImagesCatalog {
	struct Posts {
		static var Star: UIImage { return UIImage(named: "Star")! }
	}
	static var LaunchIcon: UIImage { return UIImage(named: "LaunchIcon")! }
	struct Main {
		static var SearchTabIcon: UIImage { return UIImage(named: "SearchTabIcon")! }
		static var ProfileTabIcon: UIImage { return UIImage(named: "ProfileTabIcon")! }
	}
}

Note that the properties are computed rather than assigned directly in order to avoid the images beind cached for the entire lifecycle of the application.

Storyboards

The generated code for storyboards contains constants for storyboard identifiers, reuse identifiers, and segue identifiers:

struct MainStoryboard {
	struct StoryboardIdentifiers {
		static let MainViewController = "MainViewController"
	}
	struct ReuseIdentifiers {
		static let PostTableViewCell = "PostTableViewCell"
		static let CommentTableViewCell = "CommentTableViewCell"
	}
	struct SegueIdentifiers {
		static let MainToDetail = "MainToDetail"
	}
}

Color Lists

Color lists can be created and edited visually by using the OS X color picker or programmatically using the NSColorList class. swiftrsrc automatically handles the task of converting colors to the appropriate color space depending on the platform that the code is being generated for.

struct AppLightColorList {
	static let Blue = UIColor(red: 0.045, green: 0.549, blue: 0.995, alpha: 1.000)
	static let Red = UIColor(red: 0.998, green: 0.261, blue: 0.321, alpha: 1.000)
	static let Orange = UIColor(red: 0.986, green: 0.525, blue: 0.060, alpha: 1.000)
}

Contact

License

swiftrsrc is licensed under the MIT License. See LICENSE for more information.