Enum variables in Swift?
For Swift enum
, you can only use (String|Integer|Float)LiteralConvertible
types as the raw value. If you want to use existing type(e.g. CGPoint
) for the raw value, you should follow @Alex answer.
I will provide 2 alternatives in this answer
Very simple solution
enum Test: String {
case A = "foo:1"
case B = "bar:2"
var var1: String {
return split(self.rawValue, { $0 == ":" })[0]
}
var var2: Int {
return split(self.rawValue, { $0 == ":" })[1].toInt()!
}
}
let test = Test.A
println(test.var1) // -> "foo"
You don't like this? go to next one :)
Behavior emulation using struct
and static
constants
struct Test {
let var1: String
let var2: Int
private init(_ var1:String, _ var2:Int) {
self.var1 = var1
self.var2 = var2
}
}
extension Test {
static let A = Test("foo", 1)
static let B = Test("bar", 2)
static let allValues = [A, B]
}
let test = Test.A
println(test.var1) // -> "foo"
But of course, struct
lacks some features from enum
. You have to manually implement it.
Swift enum
implicitly conforms Hashable
protocol.
extension Test: Hashable {
var hashValue:Int {
return find(Test.allValues, self)!
}
}
func ==(lhs:Test, rhs:Test) -> Bool {
return lhs.var1 == rhs.var1 && lhs.var2 == rhs.var2
}
Test.A.hashValue // -> 0
Test.B.hashValue // -> 1
Test.A == Test.B // -> false
In the first code, we already have allValues
that is corresponding to values()
in Java. valueOf(...)
in Java is equivalent to init?(rawValue:)
in RawRepresentable
protocol in Swift:
extension Test: RawRepresentable {
typealias RawValue = (String, Int)
init?(rawValue: RawValue) {
self.init(rawValue)
if find(Test.allValues, self) == nil{
return nil
}
}
var rawValue: RawValue {
return (var1, var2)
}
}
Test(rawValue: ("bar", 2)) == Test.B
Test(rawValue: ("bar", 4)) == nil
And so on...
I know this is not "in a generic way". And one thing we never can emulate is "Matching Enumeration Values with a Switch Statement" feature in Swift. you always need default
case:
var test = Test.A
switch test {
case Test.A: println("is A")
case Test.B: println("is B")
default: fatalError("cannot be here!")
}
How to declare a typedef enum of structs in Objective-C if possible
You can declare enum only of integral type, so struct or any other composite type is not allowed here. In your case const CGPoint is probably the best option:
const CGPoint kFeedbackPositionMiddle = {20.f,80.f};
...
I want my custom Error type has name Error
The name of the module is Swift
, with a capital "S".
enum Error: Swift.Error {
}
@IBInspectable with enum?
That's not possible (for now). You can only use those types that you see in User Defined Runtime Attributes section.
From Apple's doc:
You can attach the IBInspectable attribute to any property in a class declaration, class extension, or category for any type that’s supported by the Interface Builder defined runtime attributes: boolean, integer or floating point number, string, localized string, rectangle, point, size, color, range, and nil.
Related Topics
Private Var Is Accessible from Outside the Class
Swift - Firebase Search in Database
How to Avoid Nested Navigation Bars in Swiftui
Could Not Find an Overload for '/' That Accepts the Supplied Arguments
How to Make Keyboard Dismiss When I Press Out of Searchbar on Swift
Swift Parsing Attribute Name for Given Elementname
Uiscrollview with Embedded Uiimageview; How to Get the Image to Fill the Screen
Missing Return in a Function Expected to Return 'Double'
Convert Generic Free Function into Array Extension
How to Install Xcode on an External Hard Drive Along with the iPhone Simulator.App
How to Make the Memberwise Initialiser Public, by Default, for Structs in Swift
Custom Swift Encoder/Decoder for the Strings Resource Format
How to Detect Vertical Planes in Arkit
Implicitly Lazy Static Members in Swift
Check If Variable Is an Optional, and What Type It Wraps
Scenekit Scnscenerendererdelegate - Renderer Function Not Called