Skip to content

Instantly share code, notes, and snippets.

@aronbudinszky
Created March 6, 2020 15:31
Show Gist options
  • Save aronbudinszky/7d002e3d564a55e4a2ca517afa61fec1 to your computer and use it in GitHub Desktop.
Save aronbudinszky/7d002e3d564a55e4a2ca517afa61fec1 to your computer and use it in GitHub Desktop.
Support named assets via enum values (to avoid string literals).
import Foundation
import SwiftUI
/// Extend Image with named assets
extension Image {
enum Assets: String, RawRepresentable {
case appButtonImage
/// Place all your color assets here...the enum case should equal to the name you use in your Asset library
}
init(asset: Image.Assets) {
self.init(asset.rawValue)
}
}
/// Extend UIImage with named assets
extension UIImage {
convenience init(asset: Image.Assets) {
// Force unwrap okay because we are sure that Image.Assets are available
self.init(named: asset.rawValue)!
}
}
/// In use...
let image = Image(asset: .appButtonImage)
let uiImage = UIImage(asset: .appButtonImage)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment