Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add documentation to enum associated values in Swift

Tags:

xcode

swift

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.

like image 887
WetFish Avatar asked Sep 24 '16 14:09

WetFish


People also ask

What is associated values in enum Swift?

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.

Why Swift enums with associated values Cannot have a raw value?

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.

Can you give useful examples of enum associated values?

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 .

Can we write function inside enum Swift?

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.


1 Answers

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: enter image description here

like image 101
jqgsninimo Avatar answered Oct 07 '22 09:10

jqgsninimo