Is there a way to add descriptions to associated values of enums in Swift 3? I want them to show up in the symbol documentation popup (option+click), like they do for function parameters in Xcode 8.
This is my enum:
enum Result {
/**
Request succeeded.
- Parameters:
- something: Some description.
- otherThing: Other description.
*/
case Success(something: Int, otherThing: Int)
/**
Request failed.
- Parameter error: Error.
*/
case Error(error: Error)
}
I tried using - Parameters:
, but it doesn't work in enums.
This additional information is called an associated value, and it varies each time you use that case as a value in your code. You can define Swift enumerations to store associated values of any given type, and the value types can be different for each case of the enumeration if needed.
A Swift enum can either have raw values or associated values. Why is that? It's because of the definition of a raw value: A raw value is something that uniquely identifies a value of a particular type. “Uniquely” means that you don't lose any information by using the raw value instead of the original value.
These additional information attached to enum values are called associated values. Let's see an example, enum Distance { // associate value case km(String) ... } Here, (String) is additional information attached to the value km .
Finally, let's take a look at how enum cases relate to functions, and how Swift 5.3 brings a new feature that lets us combine protocols with enums in brand new ways. A really interesting aspect of enum cases with associated values is that they can actually be used directly as functions.
Do it like this:
/// Enum Description
enum Enum {
/// enum1 Description
/// - value1: value1 Description
/// - value2: value2 Description
case enum1(value1: Int, value2: String)
/// enum2 Description
case enum2
}
Show result:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With