Better init unit from raw
This commit is contained in:
@@ -108,6 +108,16 @@ public enum Unit: String, CaseIterable, CustomStringConvertible, Codable,
|
||||
case kilocalorie = "kcal"
|
||||
case kilojoule = "kJ"
|
||||
|
||||
public init?(rawValue: String) {
|
||||
if let unit = Unit.allCases.first(where: {
|
||||
$0.rawValue.lowercased() == rawValue
|
||||
}) {
|
||||
self = unit
|
||||
return
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
public var category: UnitCategory {
|
||||
switch self {
|
||||
case .kilogram, .gram, .milligram, .microgram, .pound, .ounce,
|
||||
|
||||
Reference in New Issue
Block a user