rawrepresentable

Raw type 'Bool' is not expressible by any literal

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 08:34:01
问题 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 'Bool' is not expressible by any literal That's strange, as true and false seem to be perfect candidates for expressibility by literals. I've also tried to add RawRepresentable conformance to type Bool with: extension Bool: RawRepresentable { public init?

Swift enum inheritance

泄露秘密 提交于 2019-12-17 16:05:08
问题 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 { } generates error: type 'TemperatureSubunit' does not conform to protocol 'RawRepresentable' 回答1: In Swift language, we have Structs, Enum and Classes. Struct and Enum are passed by copy but Classes are passed by reference. Only Classes support inheritance,

Enum of structs in Swift 3.0

倾然丶 夕夏残阳落幕 提交于 2019-12-01 03:16:31
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) { self.variable1 = variable1 self.variable2 = variable2 self.variable3 = variable3 } } enum AllStructs: CustomStruct { case getData case addNewData func getAPI() -> CustomStruct { switch self { case getData: return CustomStruct(variable1:"data1", variable2: SomeObject.class, variable3: POST) case addNewData: // Same to same default: return nil } } } I get the following

Enum of structs in Swift 3.0

拈花ヽ惹草 提交于 2019-12-01 00:14:25
问题 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) { self.variable1 = variable1 self.variable2 = variable2 self.variable3 = variable3 } } enum AllStructs: CustomStruct { case getData case addNewData func getAPI() -> CustomStruct { switch self { case getData: return CustomStruct(variable1:"data1", variable2: SomeObject

Enumeration with raw values

戏子无情 提交于 2019-11-30 09:26:29
问题 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) } 回答1: Because: Raw values can be strings, characters, or any of the integer or floating-point number types. But there is an alternative solution for you: enum Edges { case TopLeft case TopRight case BottomLeft case BottomRight func getTuple() -> (Double, Double) { switch self { case .TopLeft:

Enumeration with raw values

只愿长相守 提交于 2019-11-29 16:03:57
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) } Because : Raw values can be strings, characters, or any of the integer or floating-point number types. But there is an alternative solution for you: enum Edges { case TopLeft case TopRight case BottomLeft case BottomRight func getTuple() -> (Double, Double) { switch self { case .TopLeft: return (0.0, 0.0) case .TopRight: return (1.0, 0.0) case .BottomLeft: return (0.0, 1.0) case .BottomRight:

Swift enum inheritance

自闭症网瘾萝莉.ら 提交于 2019-11-28 07:55:26
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 { } generates error: type 'TemperatureSubunit' does not conform to protocol 'RawRepresentable' Korpel In Swift language, we have Structs, Enum and Classes. Struct and Enum are passed by copy but Classes are passed by reference. Only Classes support inheritance, Enum and Struct don't. So to answer your question, you can't have inheritance with Enum (and Struct