How to document an enum case with parameters?
/// Various coffee types.
enum Coffee {
/// A Cappuccino.
/// - cream: Is true if cream is added.
case cappuccino(cream: Bool)
}
It'll show up as a bullet point when you click on cappuccino
but won't show up if you click on the cream
parameter.. But it's good enough I guess..
How to test equality of Swift enums with associated values
Swift 4.1+
As @jedwidz has helpfully pointed out, from Swift 4.1 (due to SE-0185, Swift also supports synthesizing Equatable
and Hashable
for enums with associated values.
So if you're on Swift 4.1 or newer, the following will automatically synthesize the necessary methods such that XCTAssert(t1 == t2)
works. The key is to add the Equatable
protocol to your enum.
enum SimpleToken: Equatable {
case Name(String)
case Number(Int)
}
let t1 = SimpleToken.Number(123)
let t2 = SimpleToken.Number(123)
Before Swift 4.1
As others have noted, Swift doesn't synthesize the necessary equality operators automatically. Let me propose a cleaner (IMHO) implementation, though:
enum SimpleToken: Equatable {
case Name(String)
case Number(Int)
}
public func ==(lhs: SimpleToken, rhs: SimpleToken) -> Bool {
switch (lhs, rhs) {
case let (.Name(a), .Name(b)),
let (.Number(a), .Number(b)):
return a == b
default:
return false
}
}
It's far from ideal — there's a lot of repetition — but at least you don't need to do nested switches with if-statements inside.
Swift enum size when associated value is a reference type
From Type Layout: Single-Payload Enums:
If the data type's binary representation has extra inhabitants, that is, bit patterns with the size and alignment of the type but which do not form valid values of that type, they are used to represent the no-data cases, with extra inhabitants in order of ascending numeric value matching no-data cases in declaration order.
Your example with more cases:
enum Opt<T> {
case a, b, c, d, e, f, g, h, i, j, k
case l, m, n, o, p, q, r, s, t, u, v
case some(T)
}
class Person {
var name: String
init(name: String) { self.name = name }
}
print(unsafeBitCast(Opt<Person>.a, to: UnsafeRawPointer.self))
// 0x0000000000000000
print(unsafeBitCast(Opt<Person>.b, to: UnsafeRawPointer.self))
// 0x0000000000000002
print(unsafeBitCast(Opt<Person>.v, to: UnsafeRawPointer.self))
// 0x000000000000002a
let p = Person(name: "Bob")
print(unsafeBitCast(Opt.some(p), to: UnsafeRawPointer.self))
// 0x00006030000435d0
Apparently, 0x0
, 0x2
, ..., 0x2a
are invalid bit patterns for a pointer, and therefore used for the additional cases.
The precise algorithm seems to be undocumented, one probably would have to inspect the Swift compiler source code.
Swift: Unable to switch on enum associated value
The problem is that you’re passing an Int16 value to the switch. You’re setting entry.mood, an Int16, to the raw value 1, but the switch wants your Mood type. So you have a type mismatch.
You can solve it by turning the value into a Mood:
switch Mood(rawValue: entry.mood)! {
How to compare enum with associated values by ignoring its associated value in Swift?
Edit: As Etan points out, you can omit the (_)
wildcard match to use this more cleanly:
let number = CardRank.Number(5)
if case .Number = number {
// Is a number
} else {
// Something else
}
Unfortunately, I don't believe that there's an easier way than your switch
approach in Swift 1.2.
In Swift 2, however, you can use the new if-case
pattern match:
let number = CardRank.Number(5)
if case .Number(_) = number {
// Is a number
} else {
// Something else
}
If you're looking to avoid verbosity, you might consider adding an isNumber
computed property to your enum that implements your switch statement.
Related Topics
How Does Appdelegate.Swift Replace Appdelegate.H and Appdelegate.M in Xcode 6.3
How to Open to a Specific View Using Home Quick Actions
Swiftui Pass Two Child Views to View
Get Compiler Error in Swift Indexof()
Swift Callkit Sometimes Can't Activate Loudspeaker After Received Call (Only Incoming Call)
Difference Between Optional and Forced Unwrapping
Cannot Call Value of Non-Function Type 'Nshttpurlresponse' Alamofire Objectmapper
How to Use Bit Field with Swift to Store Values with More Than 1 Bit
Swift 3 Issue with Cvararg Being Passed Multiple Times
How to Properly Check If Non-Optional Return Value Is Valid
Swift Codable with Different Array Types
Swift: Search Bar Created at Auto Focus
Swiftui: Localizedstringkey with Indices
How to Segue Values When My Viewcontroller Is Embedded in an Uinavigationcontroller
Firestore Query Across Multiple Fields for Same Value