Questions tagged [rawrepresentable]
27 questions
86
votes
3 answers
Swift enum inheritance
Can you inherit enum in Swift? What are the rules that one should be aware of with regards to enum inheritance?
The following test code:
enum TemperatureUnit: Int {
case Kelvin, Celcius, Farenheit
}
enum TemperatureSubunit : Temperature { …

Boon
- 40,656
- 60
- 209
- 315
17
votes
4 answers
In Swift, how can I specify a typealias that restricts RawRepresentable to String?
I'm trying to define a protocol that requires an enum with raw value String to be implemented.
I don't believe that it's currently possible to enforce the use of enum, and I'm not sure I really care as long as somewhere down the line I can call…

Paul Young
- 1,489
- 1
- 15
- 34
15
votes
5 answers
Enum of structs in Swift 3.0
I am trying to create an enum of a struct that I would like to initialize:
struct CustomStruct {
var variable1: String
var variable2: AnyClass
var variable3: Int
init (variable1: String, variable2: AnyClass, variable3: Int) {
…

Siddharth
- 9,349
- 16
- 86
- 148
14
votes
5 answers
Raw type 'Bool' is not expressible by any literal
I want to have my enums easily compatible with @IBInspectable, so for the sake of simplicity, I tried to have it representable with type Bool:
enum TopBarStyle: Bool {
case darkOnLight
case lightOnDark
}
But Xcode is giving me:
Raw type…

Cœur
- 37,241
- 25
- 195
- 267
12
votes
5 answers
Type 'Error' does not conform to protocol 'RawRepresentable'
Changing my playground code to Swift 3, Xcode suggested changing
enum Error: ErrorType {
case NotFound
}
to
enum Error: Error {
case NotFound
}
but now I get the title error and I don't know how to get the enum to conform to that protocol.

Shades
- 5,568
- 7
- 30
- 48
6
votes
2 answers
Defining a Swift Protocol for Arbitrary, Int-based Enums
I have this enumeration representing a color, and I have added several methods to conveniently obtain new instances based on arithmetic operations on the original's raw value:
enum Color : Int
{
case Red = 0
case Green
case Blue
…

Nicolas Miari
- 16,006
- 8
- 81
- 189
4
votes
1 answer
How to make protocol describing string-representable enums?
I've got a method which calls method of some manager to save int value with some key. My method receives int and some EnumKey enum value as a key, extrudes EnumKey's rawValue and passes it to a manager as a string:
set(value: Int, forKey key:…

abjurato
- 1,439
- 11
- 17
3
votes
1 answer
How to use generic default parameters
This is my code:
class Person {
init(raw: T = Child.johnDoe) {}
}
enum Child: String {
case johnDoe
}
It doesn't compile. The error is:
Default argument value of type 'Child' cannot be converted to type 'T'
Why can't…

J. Doe
- 12,159
- 9
- 60
- 114
3
votes
2 answers
Enumeration with raw values
Why I can't define enumeration with raw values like this?
enum Edges : (Double, Double) {
case TopLeft = (0.0, 0.0)
case TopRight = (1.0, 0.0)
case BottomLeft = (0.0, 1.0)
case BottomRight = (1.0, 1.0)
}
user4410715
3
votes
1 answer
How can I create an instance of a generic enum in Swift
I'm trying to write a function in swift that creates a rawValue enum in a generic function like this:
enum STATE: String {
case OK = "OK"
case ERROR = "ERROR"
}
func createEnum(rawValue: T.Type) {
return E(rawValue:…

bjnwagner
- 65
- 1
- 1
- 6
2
votes
1 answer
Objective-C enums conform to RawRepresentable
Any exprerience on this I have for example the following enum written in objc
typedef enum {
Type1,
Type2
} Type;
extension Type: RawRepresentable {
typealias RawValue = UInt32
}
compiler crashes when I'm trying to conform to…

sger
- 719
- 2
- 12
- 26
2
votes
1 answer
Objc visible string enum but not RawRepresentable
I want to use enum that is visible both in objective C and Swift but not conform to protocol RawRepresentable.
I tried to have an enum of string both visible in Objc and Swift thus I use
typedef NSString *myEnum NS_TYPED_ENUM;
I tried to take…

angli1937
- 53
- 4
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 specify the type of a function parameter with combined type CustomStringConvertible and RawRepresentable?
I want a generic function that can instantiate object of a few different enum types I have by supplying the enum type and the Int raw-value. These enums are also CustomStringConvertible.
I tried this:
func myFunc(type: CustomStringConvertible.Type &…

meaning-matters
- 21,929
- 10
- 82
- 142
1
vote
2 answers
Is there a way to simplify this 'matrix of overloads' based on argument types which are all ultimately representable by a specific type?
We're trying to create a function addQueryItem which ultimately uses a string and an optional string internally.
For more flexibility in the API, rather than use String for the argument types, we are instead using CustomStringConvertible (which…

Mark A. Donohoe
- 28,442
- 25
- 137
- 286