Questions tagged [associated-value]
34 questions
61
votes
6 answers
How to make a Swift enum with associated values equatable
I have an enum of associated values which I would like to make equatable for testing purposes, but do not know how this pattern would work with an enum case with more than one argument.
For example, summarised below I know the syntax for making…
user1162328
27
votes
4 answers
Use "or" logic with multiple "if case" statements
Suppose I have an enum case with an associated value, and two variables of that enum type:
enum MyEnum {
case foo, bar(_ prop: Int)
}
let var1 = MyEnum.foo
let var2 = MyEnum.bar(1)
If I want to check if both variables match the general case…

John Montgomery
- 6,739
- 9
- 52
- 68
5
votes
1 answer
Using enums to design error types in Swift
Maybe I'm just completely overthinking this but I am trying to use enums to handle errors from an API I am integrating with.
From the swagger documentation of this API I can see all of the possible responses that could be returned. I have written…

sargturner
- 540
- 2
- 18
4
votes
3 answers
Swift - find object in array of enums with associated values
I have this enum:
enum Animal {
case cat(CatModel)
case dog(DogModel)
}
And an array of animals:
var animals: [Animal]
I need to find a Cat object in this array by a property that Dog doesn't have. litterBoxId for example.
let cat =…

soleil
- 12,133
- 33
- 112
- 183
4
votes
2 answers
Associated enum state in SwiftUI
How can I use an associated enum as a @State variable in an if statement in SwiftUI?
struct ProfileView: View {
@State private var choice = Choice.simple
private enum Choice {
case simple
case associated(Int)
}
var…

Edward Brey
- 40,302
- 20
- 199
- 253
4
votes
1 answer
What is the easiest way to test if an enum-based variable is *not* equal to a specific case with an associated value?
We have the following enum and variable
enum DisplayState{
case loading
case loaded(ViewModel)
case noResults
case error
}
var displayState:DisplayState = .loading
We want to test if we're in any state other than loaded.
Since…

Mark A. Donohoe
- 28,442
- 25
- 137
- 286
3
votes
1 answer
Enums with Associated Values in Python
In Swift Enums have something called an Associated Value. Consider the following:
enum Shape {
case circle(radius: Double)
case rectangle(x: Double, y: Double)
}
In this case the Enums' Associated Values are radius and x & y and can be…

Jay
- 59
- 6
3
votes
2 answers
Nested enums to allow multiples of timeframes
I'm hoping to achieve a certain style of enum syntax/functionality, however I'm not sure how to achieve it. Currently I have the following:
internal enum Timeframe: Equatable {
// MARK: - Cases
case hour(count: Int)
case day(count:…

user3746428
- 11,047
- 20
- 81
- 137
3
votes
1 answer
swift enum mirror get associated value name
I need to get the name of enum associated value.
for example:
enum App{
case iOS(version:String)
case android(version:String, build:Int)
}
let iosApp = App.iOS(version:"2.30.11")
let androidApp = App.android(version:"2.30.11",build:101)
let…

user2967434
- 31
- 3
2
votes
2 answers
Is there a way in Swift to get an associated value without using a switch statement?
When I have a situation where I already know enum case statement I want to get the associated value of, is there a cleaner way than using a switch statement to pluck out the associated value?
To have to come up with a switch statement, provide…

clearlight
- 12,255
- 11
- 57
- 75
2
votes
1 answer
Enum associated value confusing
When I try use func obj func, I get the error:
Cannot invoke 'obj' with an argument list of type '(message: (QueueAddable))'
I'm confused with Swift types.
Obj func used to get concrete type for decode.
protocol QueueAddable: Encodable {
var…

Drugan
- 144
- 7
2
votes
2 answers
Swift Switch case on enum with Classes
This is the first time I use this kind of enums, enum with associated value type, I need to make a switch statement depending on the object's type, I cannot managed to do it, this is the enum:
enum TypeEnum {
case foo(FooClass)
case…

user2434385
- 281
- 1
- 3
- 16
2
votes
1 answer
Swift Use enum with custom answer
I want to use an enum in Swift for some stuff like subjects in school. And if someone wants to have another subject, which isn't in the enum, he can type in the subject as a custom value.
For example:
enum Subjects {
case Math
case German
…

Hemmelig
- 803
- 10
- 22
1
vote
1 answer
In Rust can you associate a constant value to each enum variant and then collect those values at compile time?
Let's say I have an enum, and I want to somehow annotate or associate each variant with a &str. E.g.:
enum MyEnum {
A, // "foo"
B(String), // "bar"
C(i32) // "baz"
}
I would also like to collect these &str's into an array like
const…

Jack Stodart
- 13
- 3
1
vote
1 answer
Avoiding `(())` to explicitly specify an enum's associated value of type Void
Let's say I have something similar to the standard Result type:
enum TestResult {
case success(SuccessData)
case failure(FailureData)
}
That's fine in general, when SuccessData and FailureData are 'real' types -…

Wade Tregaskis
- 1,996
- 11
- 15