Questions tagged [associated-value]

34 questions
1
vote
1 answer

Generic Class with Enum with associated values

I'm trying to create a generic UITableViewCell that inherits from an enum with associated values. This is what I tried: Enum declaration: enum Content: Equatable { case text(String) case image(UIImage) case error(Error) case…
Roi Mulia
  • 5,626
  • 11
  • 54
  • 105
1
vote
1 answer

Is there a clean way of making an enum with associated value conform to rawRepresentable?

I have this in my code and it works, however if I have other enums (not necessarily color) with a long list it gets tiresome. Is there a better way of having an enum with an associated value that also conforms to RawRepresentable? public enum…
Aaron Bratcher
  • 6,051
  • 2
  • 39
  • 70
1
vote
1 answer

How to declare an `enum` of `enum` that allows for "flattened" `switch` statement?

Optional in Swift allows for magic where switch-ing over an optional enum flattens the cases into a single switch statement. Given: enum Foo { case bar, baz } You can: let foo: Foo? = .bar switch foo { case .bar: break case .baz: …
Isaaс Weisberg
  • 2,296
  • 2
  • 12
  • 28
1
vote
0 answers

Python panda multi-index dataframes: How to associate specific columns to a specific value from a new index

Python panda multi-index dataframes: How to associate specific columns to a specific value from a new index I have meteorological data in a multi-index panda dataframe. For simplicity let’s limit the example to temperatures at different pressure…
isbjörnen
  • 11
  • 1
1
vote
1 answer

Swift - Nested enums - how to perform same action on all nested enums with associated values?

I want to print the raw value of nested enums. For example, if I have a top level Food enum with multiple cases (for simplicity let's just say two: fruit, veggie), each of which are string enums (Fruit: String, Vegetable: String, etc), is there a…
Nobadi
  • 121
  • 2
  • 13
1
vote
1 answer

Unwrapping associated value for all cases in switch

I have an enum similar to this, where all cases contain the same associated value content: enum RowType { case single(_ content: [Any]) case double(_ content: [Any]) case triple(_ content: [Any]) ... } I know I could just use a…
emmics
  • 994
  • 1
  • 11
  • 29
1
vote
1 answer

Enums associated values and generics

I have a enum like this, it contains different initial states: enum InitialState { case listTableView(ListTableViewState) } I want to use them like this: var tableViewState: ListTableViewState? let test = ListTableViewState(group: .large,…
PaFi
  • 888
  • 1
  • 9
  • 24
1
vote
0 answers

In Swift declare protocol variable that has to conform to at least one protocol

How can I define a variable in a protocol in Swift4 that at least needs to conform to a protocol, but can also conform to other protocols as well? For example I now get an error if the protocol declares that a variable should conform to protocol B…
Jeroen
  • 11
  • 3
1
vote
2 answers

How can you do a simple inline test for an enumeration's case that has associated values you don't care about?

Given this code: class Item{} func foo(item:Item){} enum SelectionType{ case single(resultsHandler:(Item)->Void) case multi(resultsHandler:([Item])->Void) } var selectionType:SelectionType = .single(resultsHandler:foo) // This line won't…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
0
votes
0 answers

What is a good way to retrieve the associated value from a struct's enum property when the associated value might have multiple possible types?

I have an array of structs that has been decoded from a JSON file. Each struct has a stored property that is a variable-dimension array stored in an enum associated value. Some of the structs have property Enum.array2D([[Float]]) and others have…
whaleshark
  • 65
  • 6
0
votes
1 answer

Assign associated non mutable constant data to enum variants

Note: My question is similar to this question, but doesn't seem to solve my use case. Can enum variants have constant associated values? Problem explanation My server returns a country (very long list) which internally is mapped to an enum (there is…
Avba
  • 14,822
  • 20
  • 92
  • 192
0
votes
0 answers

Using Enums’ associated values to link cases from first Enum to 2nd, then call an associated function value from the CaseIterable second

I’m building a weather app that will provide access to multiple weather providers from which I get slightly-to-very different data sets returned. I want to make the system that handles the setting up of the data to be as generic as possible to…
Barrrdi
  • 902
  • 13
  • 33
0
votes
1 answer

Cakephp4, how to load associated data in an Entity object?

I have a Users table and a Roles table. A user has one role. So in UsersTable.php: $this->belongsTo('Roles', [ 'foreignKey' => 'role_id', 'joinType' => 'INNER', ]); Now in User.php (Entity!!) I need the role name…
Ruud
  • 45
  • 4
0
votes
1 answer

Swift Enum How to conform to a protocol's variables' set & get?

my code: public protocol ApiRequestBaseObjProtocol { var param:[String:Any] { get set } var path:String {get} } extension ApiRequestBaseObjProtocol { var param: [String : Any] { get { var key = "\(self.path)" …
0
votes
2 answers

Enumeration on enums with associated values - Swift

I have an enum with associated values. In addition, every value has a String description. How can I get description of all the cases? enum MyEnum { case caseA(data: [DataOfTypeA]) case caseB(data: [DataOfTypeB]) case caseC(data:…
Luda
  • 7,282
  • 12
  • 79
  • 139