Created
March 6, 2020 15:31
-
-
Save aronbudinszky/7d002e3d564a55e4a2ca517afa61fec1 to your computer and use it in GitHub Desktop.
Support named assets via enum values (to avoid string literals).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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