Skip to content

Instantly share code, notes, and snippets.

@noorulain17
Last active March 18, 2021 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noorulain17/2b32048ed066d6545c8db61d072a7039 to your computer and use it in GitHub Desktop.
Save noorulain17/2b32048ed066d6545c8db61d072a7039 to your computer and use it in GitHub Desktop.
swift-documentation

Sample class for SwiftDocumentation :-

Created by Noor ul Ain Ali on 13/02/2021.


Variables

Adding code with Summary and Discussion

area

/**
The area of the `Shape` instance.

Computation depends on the shape of the instance.
For a triangle, `area` is equivalent to:

   let height = triangle.calculateHeight()
   let area = triangle.base * height / 2
*/
var area: CGFloat { get { return 5 } }

Adding code with Summary and Discussion

perimeter

/**
The perimeter of the `Shape` instance.

Computation depends on the shape of the instance, and is
equivalent to:

  ~~~
  // Circles:
  let perimeter = circle.radius * 2 * Float.pi

  // Other shapes:
  let perimeter = shape.sides.map { $0.length }
                             .reduce(0, +)
  ~~~
*/
var perimeter: CGFloat { get { return 5 } }

Methods

Adding list (ordered and unordered) with Summary and Discussion

thisIsFirstMethod

/**
# Lists

You can apply *italic*, **bold**, or `code` inline styles.

## Unordered Lists

- Lists are great,
- but perhaps don't nest;
- Sub-list formatting...
    - ...isn't the best.

## Ordered Lists

1. Ordered lists, too,
2. for things that are sorted;
3. Arabic numerals
4. are the only kind supported.
*/

func thisIsFirstMethod() {
}

Adding Parameter, Throws and Return separate sections

greeting

/**
Creates a personalized greeting for a recipient.

- Parameter recipient: The person being greeted.
- Throws: `MyError.invalidRecipient`
      if `recipient` is "Noor"
      (`host` cannot be `recipient`).
- Returns: A new string saying hello to `recipient`.
*/
func greeting(to recipient: String) throws -> String {
   guard recipient != "Noor" else {
       throw NSError(domain: "InvalidRecipient", code: 500, userInfo: nil)
   }

   return "Greetings, \(recipient)!"
}

Adding Multiple Parameters description and Return sections

magnitude3D

/// Returns the magnitude of a vector in three dimensions
/// from the given components.
///
/// - Parameters:
///     - x: The *x* component of the vector.
///     - y: The *y* component of the vector.
///     - z: The **z** component of the vector.
func magnitude3D(x: Double, y: Double, z: Double) -> Double {
    return sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2))
}

Adding simple autogenerated documentation comment from Xcode -> Editor -> Structure -> Add Documentation

newMethod

/// This is autogenerated documentation for `newMethod`
func newMethod() {
}

Adding simple autogenerated documentation with parameter comment from Xcode -> Editor -> Structure -> Add Documentation

newMethodWithInput

/// This is autogenerated with parameter
/// - Parameter inputValue: any string value
func newMethod(with inputValue: String) {        
}

Adding simple autogenerated documentation with parameter & return from Xcode -> Editor -> Structure -> Add Documentation

newMethodWithInput-Return

/// This is autogenerated with parameter & return value
/// - Parameter inputValue: any string value
/// - Returns: same as inputValue
func newMethod(with inputValue: String) -> String {
    return inputValue
}

All available predefined sections in Xcode documentation

extraFields-1

extraFields-2

 /**
   Additional fields

- Callout: Add Callout description that you can use
- Attention: ...
- Author: ...
- Authors: ...
- Bug: ...
- Complexity: ...
- Copyright: ...
- Date: ...
- Experiment: ...
- Important: ...
- Invariant: ...
- Note: ...
- Postcondition: ...
- Precondition: ...
- Remark: ...
- Requires: ...
- See: [Google link here](https://www.google.com) ...
- Since: ...
- Todo: ...
- Version: ...
- Warning: ...
*/

func extraFields() {
}

Adding Callout section with indented list

extraFields2

 /**
 Additional fields
 
 - Callout: Add Callout description that you can use
   - hi
   - there
 */
 
func extraFields2() {
}

Markdown-cheatsheet

For more information regarding Markdown syntax, refer (this)[https://github.com/tchapi/markdown-cheatsheet/blob/master/README.md]

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment