Last active
May 18, 2022 12:43
-
-
Save ekscrypto/4e289f5bccaeaabf78b761266e6ddf56 to your computer and use it in GitHub Desktop.
RawRepresentable ~ prefix
This file contains 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
enum AppStorageKeys: String { | |
case activeEnvironment | |
} |
This file contains 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
extension RawRepresentable { | |
/// ~ | |
/// | |
/// Convenience operator to allow ~something instead of something.rawValue | |
static prefix func ~(rhs: Self) -> Self.RawValue { | |
rhs.rawValue | |
} | |
} |
This file contains 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
// Typical way of extracting String value from enum | |
let myEnvironmentKey: String = AppStorageKeys.activeEnvironment.rawValue | |
// Using the prefix | |
let myEnvironmentKey: String = ~AppStorageKeys.activeEnvironment | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment