Using `if-case` format in boolean assignment in Swift? - swift

If I have the following enum defined:
enum CardRank {
case number(Int)
case jack
case queen
case king
case ace
}
I know I can use an if-let to check if it's a number:
let cardRank = CardRank.number(5)
if case .number = cardRank {
// is a number
} else {
// something else
}
Instead of using an if statement, though, I want to assign the boolean result of "is this a number" to a variable.
E.g. something like this:
let isNumber = (case .number = cardRank)
However, this gives me the compiler error:
error: expected expression in list of expressions
let isNumber = (case .number = cardRank)
^
Is there a similar 1-line syntax to get this kind of assignment to work?
The closest I got was this, but it's pretty messy:
let isNumber: Bool = { if case .number = cardRank { return true } else { return false } }()
I know I can implement the Equatable protocol on my enum, and then do the following:
let isAce = cardRank == .ace
But I'm looking for a solution that doesn't require needing to conform to the Equatable protocol.

Add it as a property on the enum:
extension CardRank {
var isNumber: Bool {
switch self {
case .number: return true
default: return false
}
}
}
let isNumber = cardRank.isNumber

Related

How can I compare two enums with OR operator?

I need to compare if any of the two cases meets the requirement, I need to take decision else need to do something else. I tried many ways and this is one of them but every way giving error.
if case .locked = itemStatus || case .hasHistoryLocked = itemStatus {
print("YES")
} else {
print("NO")
}
A switch is common pattern for matching a series of cases. See The Swift Programming Language: Enumerations: Matching Enumeration Values with a Switch Statement.
E.g.:
switch itemStatus {
case .locked, .hasHistoryLocked:
print("YES")
default:
print("NO")
}
If you want to add this in an if or guard statement, you can wrap the above in a computed property. E.g.,
extension ItemStatus {
var isLocked: Bool {
switch self {
case .locked, .hasHistoryLocked:
return true
default:
return false
}
}
}
Then you can do things like:
func doSomethingIfUnlocked() {
guard !itemStatus.isLocked else {
return
}
// proceed with whatever you wanted if it was unlocked
}
Alternatively, you can add Equatable conformance for this type. So, imagine ItemStatus was defined like so:
enum ItemStatus {
case locked
case hasHistoryLocked
case unlocked(Int)
}
Now, if this was your type, you could just add Equatable conformance:
enum ItemStatus: Equatable {
case locked
case hasHistoryLocked
case unlocked(Int)
}
If it was not your type and you can not simply edit the original declaration, you could instead add Equatable conformance:
extension ItemStatus: Equatable {
static func == (lhs: Self, rhs: Self) -> Bool {
switch (lhs, rhs) {
case (.locked, .locked), (.hasHistoryLocked, .hasHistoryLocked): // obviously, add all cases without associated values here
return true
case (.unlocked(let lhsValue), .unlocked(let rhsValue)) where lhsValue == rhsValue: // again, add similar patterns for all cases with associated values
return true
default:
return false
}
}
}
However you add Equatable conformance to ItemStatus, you can then do things like:
func doSomethingIfUnlocked() {
guard itemStatus != .locked, itemStatus != .hasHistoryLocked else {
return
}
// proceed with whatever you wanted if it was unlocked
}
As your comment I see that you don't want to use Equatable to check enum. There is another way to check it using Enum rawValue
So it just like you get the rawValue of enum from the key and compare it with your itemStatus
enum Test : Int {
case locked = 0
case hasHistoryLocked = 1
case anything = 2
}
let itemStatus = 0
if itemStatus == Test.locked.rawValue || itemStatus == Test.hasHistoryLocked.rawValue {
print("Yes")
} else {
print("No")
}

Can you initialize an Enum value from the name of its case (*not* its RawValue?)

Consider this enumeration (note its type is Int)
enum MyTestEnum : Int{
case one = 1
case eight = 8
case unknown = -1
}
You can easily initialize a version of this based on the raw value, like so...
let value = MyTestEnum(rawValue:-1)
I'm trying to find out if it can be initialized with a string representation of the case name itself (again, not the raw value, but the word after 'case') like so...
let value = MyTestEnum(caseName:"eight")
Note: I want this to work with any enum if possible, regardless of its raw value type. For instance, this one...
enum MyOtherEnum{
case xxx
case yyy
case zzz
}
let value = MyOtherEnum(caseName:"xxx")
So can this be done?
Thoughts:
I think Swift has a way to instantiate a class given a string representing the fully-qualified class-name. Perhaps something similar can be used here.
In Swift 4.2, this is quite easy to do now using CaseIterable.
enum MyOtherEnum: CaseIterable {
case xxx
case yyy
case zzz
init?(caseName: String) {
for value in MyOtherEnum.allCases where "\(value)" == caseName {
self = value
return
}
return nil
}
}
enum MyTestEnum: Int, CaseIterable{
case one = 1
case eight = 8
case unknown = -1
init?(caseName: String) {
for value in MyTestEnum.allCases where "\(value)" == caseName {
self = value
return
}
return nil
}
}
What I am doing here is creating a failable initializer which iterates through all potential cases, testing to see if "\(value)" (which returns the name for that potential case) matches the caseName argument passed in to the initializer.
When a match is found, self is set and the loop ends. Otherwise, nil is returned for the call.
Below, are two working and two failing examples:
let myOtherEnum = MyOtherEnum(caseName:"xxx")
print(myOtherEnum) // MyOtherEnum.xxx
let myTestEnum = MyTestEnum(caseName:"eight")
print(myTestEnum?.rawValue) // 8
let myOtherEnumFail = MyOtherEnum(caseName:"aaa")
print(myOtherEnumFail) // nil
let myTestEnumFail = MyTestEnum(caseName:"ten")
print(myTestEnumFail) // nil
Based on CodeBender's solution here is a nice extension for this case
extension CaseIterable {
///Note: case value and name can be different
init?(caseName: String) {
for v in Self.allCases where "\(v)" == caseName {
self = v
return
}
return nil
}
}
You can go with custom initializer
extension MyTestEnum {
public static func allValues() -> [MyTestEnum] {
let retVal = AnySequence { () -> AnyIterator<MyTestEnum> in
var raw = 0
return AnyIterator {
let current = withUnsafePointer(to: &raw) {
$0.withMemoryRebound(to: MyTestEnum.self, capacity: 1) { $0.pointee }
}
guard current.hashValue == raw else { return nil }
raw += 1
return current
}
}
return [MyTestEnum](retVal)
}
init?(caseName: String){
for v in MyTestEnum.allValues() {
if "\(v)" == caseName {
self = v
return
}
}
self = MyTestEnum.unknown
}
}
let test = MyTestEnum(caseName: "eight")
or simple manually all your case :)
extension MyTestEnum {
init?(caseName: String){
switch caseName {
case "eight": self.init(rawValue: 8)
case "one": self.init(rawValue: 1)
default: self.init(rawValue: -1)
}
}
}
let test1 = MyTestEnum(caseName: "eight")
let test2 = MyTestEnum(rawValue: 1)
Hope this helps !!
It sounds like your want your enum cases to have raw Int values and raw String values. It's not strictly possible to do this, but perhaps this comes close:
enum MyTestEnum : String {
case one
case eight
case unknown
var intValue: Int {
switch self {
case one: return 1
case eight: return 8
case unknown: return -1
}
}
}
Now you can initialize from case names, but you can also retrieve an intValue when needed. (If needed, you could easily add a failable initializer to allow initialization from integers as well.)

Swift enum get an associated value without passing through a cases [duplicate]

I've got an enumeration with a few different cases which are different types, e.g.
enum X {
case AsInt(Int)
case AsDouble(Double)
}
I can switch on these just fine to get the underlying value back out. However, the switch statement is highly annoying with trying to make me execute some code for the other cases that I simply don't care about. For example, right now I have something like
func AsInt(x: X) -> Int? {
switch x {
case AsInt(let num):
return num;
default:
return nil;
}
}
This works but it's pretty tedious always having to reference this method and having to write a new one for each case of each enumeration. What I'm looking for is how to simply attempt to cast a value of type X to one of the cases, like
var object: X = func();
let value = obj as? Int;
if value {
// do shit
}
How can I simply check for a case without having to enumerate all of the cases about which I don't care and execute some non-statement for them?
Bonus points for any solution that can declare value as part of the conditional instead of polluting the scope.
There are actually multiple ways to do it.
Let's do it by extending your enum with a computed property:
enum X {
case asInt(Int)
case asDouble(Double)
var asInt: Int? {
// ... see below
}
}
Solutions with if case
By having let outside:
var asInt: Int? {
if case let .asInt(value) = self {
return value
}
return nil
}
By having let inside:
var asInt: Int? {
if case .asInt(let value) = self {
return value
}
return nil
}
Solutions with guard case
By having let outside:
var asInt: Int? {
guard case let .asInt(value) = self else {
return nil
}
return value
}
By having let inside:
var asInt: Int? {
guard case .asInt(let value) = self else {
return nil
}
return value
}
The last one is my personal favorite syntax of the four solutions.
As of Swift 2 (Xcode 7) this is possible with if/case and
pattern matching:
let x : X = ...
if case let .AsInt(num) = x {
print(num)
}
The scope of num is restricted to the if-statement.

Get associated value from enumeration without switch/case

I've got an enumeration with a few different cases which are different types, e.g.
enum X {
case AsInt(Int)
case AsDouble(Double)
}
I can switch on these just fine to get the underlying value back out. However, the switch statement is highly annoying with trying to make me execute some code for the other cases that I simply don't care about. For example, right now I have something like
func AsInt(x: X) -> Int? {
switch x {
case AsInt(let num):
return num;
default:
return nil;
}
}
This works but it's pretty tedious always having to reference this method and having to write a new one for each case of each enumeration. What I'm looking for is how to simply attempt to cast a value of type X to one of the cases, like
var object: X = func();
let value = obj as? Int;
if value {
// do shit
}
How can I simply check for a case without having to enumerate all of the cases about which I don't care and execute some non-statement for them?
Bonus points for any solution that can declare value as part of the conditional instead of polluting the scope.
There are actually multiple ways to do it.
Let's do it by extending your enum with a computed property:
enum X {
case asInt(Int)
case asDouble(Double)
var asInt: Int? {
// ... see below
}
}
Solutions with if case
By having let outside:
var asInt: Int? {
if case let .asInt(value) = self {
return value
}
return nil
}
By having let inside:
var asInt: Int? {
if case .asInt(let value) = self {
return value
}
return nil
}
Solutions with guard case
By having let outside:
var asInt: Int? {
guard case let .asInt(value) = self else {
return nil
}
return value
}
By having let inside:
var asInt: Int? {
guard case .asInt(let value) = self else {
return nil
}
return value
}
The last one is my personal favorite syntax of the four solutions.
As of Swift 2 (Xcode 7) this is possible with if/case and
pattern matching:
let x : X = ...
if case let .AsInt(num) = x {
print(num)
}
The scope of num is restricted to the if-statement.

In Swift, is it possible to convert a string to an enum?

If I have an enum with the cases a,b,c,d is it possible for me to cast the string "a" as the enum?
Sure. Enums can have a raw value. To quote the docs:
Raw values can be strings, characters, or any of the integer or
floating-point number types
β€” Excerpt From: Apple Inc. β€œThe Swift Programming Language.” iBooks. https://itun.es/us/jEUH0.l,
So you can use code like this:
enum StringEnum: String
{
case one = "value one"
case two = "value two"
case three = "value three"
}
let anEnum = StringEnum(rawValue: "value one")!
print("anEnum = \"\(anEnum.rawValue)\"")
Note: You don't need to write = "one" etc. after each case. The default string values are the same as the case names so calling .rawValue will just return a string
EDIT
If you need the string value to contain things like spaces that are not valid as part of a case value then you need to explicitly set the string. So,
enum StringEnum: String
{
case one
case two
case three
}
let anEnum = StringEnum.one
print("anEnum = \"\(anEnum)\"")
gives
anEnum = "one"
But if you want case one to display "value one" you will need to provide the string values:
enum StringEnum: String
{
case one = "value one"
case two = "value two"
case three = "value three"
}
All you need is:
enum Foo: String {
case a, b, c, d
}
let a = Foo(rawValue: "a")
assert(a == Foo.a)
let πŸ’© = Foo(rawValue: "πŸ’©")
assert(πŸ’© == nil)
In Swift 4.2, the CaseIterable protocol can be used for an enum with rawValues, but the string should match against the enum case labels:
enum MyCode : String, CaseIterable {
case one = "uno"
case two = "dos"
case three = "tres"
static func withLabel(_ label: String) -> MyCode? {
return self.allCases.first{ "\($0)" == label }
}
}
usage:
print(MyCode.withLabel("one")) // Optional(MyCode.one)
print(MyCode(rawValue: "uno")) // Optional(MyCode.one)
In case with an enum with Int type you can do it so:
enum MenuItem: Int {
case One = 0, Two, Three, Four, Five //... as much as needs
static func enumFromString(string:String) -> MenuItem? {
var i = 0
while let item = MenuItem(rawValue: i) {
if String(item) == string { return item }
i += 1
}
return nil
}
}
And use:
let string = "Two"
if let item = MenuItem.enumFromString(string) {
//in this case item = 1
//your code
}
Riffing on djruss70's answer to create highly generalized solution:
extension CaseIterable {
static func from(string: String) -> Self? {
return Self.allCases.first { string == "\($0)" }
}
func toString() -> String { "\(self)" }
}
Usage:
enum Chassis: CaseIterable {
case pieridae, oovidae
}
let chassis: Chassis = Chassis.from(string: "oovidae")!
let string: String = chassis.toString()
Note: this will unfortunately not work if the enum is declared #objc. As far as I know as of Swift 5.3 there is no way to get this to work with #objc enum's except brute force solutions (a switch statement).
If someone happens to know of a way to make this work for #objc enums, I'd be very interested in the answer.
Swift 4.2:
public enum PaymentPlatform: String, CaseIterable {
case visa = "Visa card"
case masterCard = "Master card"
case cod = "Cod"
var nameEnum: String {
return Mirror(reflecting: self).children.first?.label ?? String(describing: self)
}
func byName(name: String) -> PaymentPlatform {
return PaymentPlatform.allCases.first(where: {$0.nameEnum.elementsEqual(name)}) ?? .cod
}
}
Extending Duncan C's answer
extension StringEnum: StringLiteralConvertible {
init(stringLiteral value: String){
self.init(rawValue: value)!
}
init(extendedGraphemeClusterLiteral value: String) {
self.init(stringLiteral: value)
}
init(unicodeScalarLiteral value: String) {
self.init(stringLiteral: value)
}
}
For Int enum and their String representation, I declare enum as follows:
enum OrderState: Int16, CustomStringConvertible {
case waiting = 1
case inKitchen = 2
case ready = 3
var description: String {
switch self {
case .waiting:
return "Waiting"
case .inKitchen:
return "InKitchen"
case .ready:
return "Ready"
}
}
static func initialize(stringValue: String)-> OrderState? {
switch stringValue {
case OrderState.waiting.description:
return OrderState.waiting
case OrderState.inKitchen.description:
return OrderState.inKitchen
case OrderState.ready.description:
return OrderState.ready
default:
return nil
}
}
}
Usage:
order.orderState = OrderState.waiting.rawValue
let orderState = OrderState.init(rawValue: order.orderState)
let orderStateStr = orderState?.description ?? ""
print("orderStateStr = \(orderStateStr)")
I used this:
public enum Currency: CaseIterable, Codable {
case AFN = 971 // Afghani (minor=2)
case DZD = 012 // Algerian Dinar (minor=2)
...
private static var cachedLookup: [String: Currency] = [:]
init?(string: String) {
if Self.cachedLookup.isEmpty {
Self.cachedLookup = Dictionary(uniqueKeysWithValues: Self.allCases.map { ("\($0)", $0) })
}
if let currency = Self.cachedLookup[string] {
self = currency
return
} else {
return nil
}
}
}
I found the other answers make this way more complicated then it needs to be. Here is a quick and concise example.
enum Gender: String {
case male, female, unspecified
}
Simple enum, note that I added ": String" to the enum itself to declare the type as string.
Now all you have to do is:
let example: Gender = Gender(rawValue: "male")
And thats it, 'example' is now an enum of type Gender with the value of .male
There is literally nothing else you need to do in Swift 4+.