Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift standard documentation comment

Tags:

swift

Is there a standard way to write documentation comment in the Swift language? Something equivalent to javadoc (Java) or docstrings (Python)?

example:

/**
 * My docstring example
 * @return the String "foo"
*/
func foo() -> String {
    return "Foo"
}
like image 220
Griffosx Avatar asked Jun 08 '14 15:06

Griffosx


People also ask

How do you comment a method in Swift?

Type /// or /** */ to begin a documentation comment and then use DocC's special dialect of Markdown to write the content. This dialect supports many keywords like - Parameters: for describing function arguments or - Returns: for describing return values.

How do I comment in Swiftui code?

There are two ways to add comments in Swift: // - Single Line comments. /*... */ - Multiline Comments.

How do I comment out code in Xcode?

6. Comments By selecting a few lines of code (pressing ⇧+↑ or ⇧+↓), you can comment a bunch of lines in a single shot with the ⌘ + / shortcut. If the lines are already comments, the same shortcut will uncomment them.

How do I add quick help in Xcode?

Xcode Quick Help Quick Help for a symbol is shown by Option-clicking a symbol. It is also shown in the Quick Help inspector in the utilities area when the insertion point is in a symbol.


3 Answers

Yes there is.

Swift includes "///" comment handling (although probably not everything yet).

Write something like:

/// Hey!
func bof(a: Int) {

}

Then option-click on the func name and voilà :)

like image 74
Jean Le Moignan Avatar answered Oct 17 '22 15:10

Jean Le Moignan


There are two types of Documentation comments single line "///..." and multiline "/**...*/" documentations NSHipster explains it here

Sample code copied from the website:

/**
    Repeats a string `times` times.

    - Parameter str:   The string to repeat.
    - Parameter times: The number of times to repeat `str`.

    - Throws: `MyError.InvalidTimes` if the `times` parameter 
      is less than zero.

    - Returns: A new string with `str` repeated `times` times.
*/

func repeatString(str: String, times: Int) throws -> String {
    guard times >= 0 else { throw MyError.InvalidTimes }
    return Repeat(count: 5, repeatedValue: "Hello").joinWithSeparator("")
}
like image 27
Khaled Annajar Avatar answered Oct 17 '22 15:10

Khaled Annajar


EDIT: This solution doesn't work with Swift 2.0.

OLDER solution work until swift 1.2:
I am now trying out the swift language and documentation tool. The Xcode is very sensitive to indent the text. The keywords must start at the same place. The keywords must insert beetwen colon, example ":returns:" If the xcode is not recognized the keyword, then Xcode puts the text into the description.

From this:

    /**
    Keresés után le lehet kérdezni egy konkrét találatot, pl. ha a harmadikra vagyunk mondjuk kíváncsiak.

    :note: n-1 legyen az \p index értéke, mert tömb révén a 0. elemmel keződik a tömb.
    :param: aSearchName Keresés meghatározásánál megadott név.
    :returns:               Konkrét találat. Ha nincs találat, akkor üres stringgel tér vissza a függvégy.
    :pre:               `aSearchName` != nil && !\p aSearchName != ""
    */

it will be:

enter image description here

like image 2
feca Avatar answered Oct 17 '22 16:10

feca