How to use Swift documentation comments
NickName:ad121 Ask DateTime:2014-12-31T11:57:16

How to use Swift documentation comments

I have a few questions about Swift documentation comments:

  1. Is there a way to make a Related declarations section like some of the Apple documentation has? For example, when I Option+Click the tablewView(_:heightForRowAtIndexPath:) method, it links me to three other related methods within the generated documentation.

  2. Is there any warning tag in Swift? I know Objective-C allowed me to do @warning and get a bolded warning in the generated documentation. However, :warning: does nothing in Swift's documentation comments, so I was curious if there was another way.

  3. Is there a way to make my documentation into an HTML file that is a similar format as the Apple Documentation? I know in other IDEs, such as Eclipse, I can generate HTML documentation for my code. Does XCode have this?

Copyright Notice:Content Author:「ad121」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/27715933/how-to-use-swift-documentation-comments

Answers
abhimuralidharan 2020-01-21T10:01:35

\n For those who want to add this as code snippet. Swift 5, XCode 11.3+\n\n\nThis is an add on to : Yogendra Singh's Answer in this thread\n\n/**\n<#Summay text for documentation#>\n\n- parameter <#parameterName#>: <#Description#>.\n- returns: <#Return values#>\n- warning: <#Warning if any#>\n\n\n # Notes: #\n 1. <#Notes if any#>\n\n # Example #\n```\n // <#Example code if any#>\n```\n\n*/\n\n\n\n Copy and paste the above code in Xcode. Select the code and then Right click.\n\n\n\n\n\n Save the code snippet and give the completion name as documentation.\n\n\n\n\n\n Now if we start typing documentation, the snippet will be shown in the code completion.\n\n\n",


Yogendra Singh 2019-03-29T11:20:00

Use the following notation for documentation comments.\n\n/**\n This method sum two double numbers and returns.\n\n Here is the discussion. This methods adds two double and return the optional Double.\n\n\n - parameter number1: First Double Number.\n - parameter number2: Second Double Number.\n - returns: The sum of two double numbers.\n\n # Notes: #\n 1. Parameters must be **double** type\n 2. Handle return type because it is optional.\n\n # Example #\n```\n if let sum = self.add(number1: 23, number2: 34) {\n print(sum)\n }\n ```\n*/\n\n\nfunc add(number1: Double, number2: Double) -> Double? {\n return number1 + number2\n}\n\n\n",


Chase McElroy 2021-12-10T00:44:01

Keyboard shortcut in Xcode\noption + cmd + /\n",


akashivskyy 2015-01-04T14:07:27

This answer was last revised for Swift 5.7 and Xcode 14.x.\n\nDocC is Apple's documentation compiler that takes comments (plus additional resources) and produces rich documentation that can be viewed in Xcode or hosted on web.\nWriting Documentation\nType /// 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.\nNote how the > Warning: keyword was recognized as an aside and automatically emphasized. DocC supports multiple other aside types like Note, Tip and Important.\n/// Produce a greeting string for the given `subject`.\n///\n/// ```\n/// print(hello("world")) // "Hello, world!"\n/// ```\n///\n/// > Warning: The returned greeting is not localized. To\n/// > produce a localized string, use ``localizedHello(_:)``\n/// > instead.\n///\n/// - Parameters:\n/// - subject: The subject to be welcomed.\n///\n/// - Returns: A greeting for the given `subject`.\nfunc hello(_ subject: String) -> String {\n return "Hello, \\(subject)!"\n}\n\n\nLinking to Symbols\nDocC will automatically link (and auto-complete!) symbols wrapped in double backticks ``. You can link to related symbols in the same type or other types in the same module.\nNote that linking is limited only to public symbols and only to one module. As of today, there's no way to type e.g. ``UIView`` and have DocC automatically link it to UIKit's documentation.\nGenerating Webpages\nDocC supports exporting your documentation into webpages. First, you need to compile your documentation by choosing Product → Build Documentation. Once the documentation is built, export its archive by clicking the More button. The archive will contain the entire documentation webpage that you can then host on your server.\nThe above process is a bit complicated, so there are many tools that can help you automate it. Apple offers swift-docc-plugin that you can add to your Swift package or Xcode project and configure it to run on every build. You can automate this process on CI as well.\nFurther Reading\nI recommend reading the following guides to learn more about DocC:\n\nWriting Symbol Documentation in Your Source Files\nFormatting Your Documentation Content\nAdding Structure to Your Documentation Pages\nDistributing Documentation to External Developers\n",


stevo.mit 2015-07-27T08:05:54

Xcode 7.0 beta 4\n\nThe notation has been changed (:param: does not work anymore ...)\n\n/// Creates the string representation of the poo with requested size.\n///\n/// - warning: Be carefull! Poos can hurt.\n/// - parameter size: requested size of the poo\n/// - returns: string representation of the poo\nfunc makePoo(size: String) -> String\n{\n return \"Ouch. This is \\(size) poo!\"\n}\n\n\nAnd it looks like this:\n\n\n\nYou can use either /// or /** */",


More about “How to use Swift documentation comments” related questions

How to use Swift documentation comments

I have a few questions about Swift documentation comments: Is there a way to make a Related declarations section like some of the Apple documentation has? For example, when I Option+Click the tabl...

Show Detail

How to use Swift documentation comments

I have a few questions about Swift documentation comments: Is there a way to make a Related declarations section like some of the Apple documentation has? For example, when I Option+Click the tabl...

Show Detail

Swift documentation comments for overriden methods?

I would like to add markup documentation to a Swift function that is implemented owing to the fact that a class conforms to UICollectionViewDataSource. For instance: /// /// - returns: Why is this

Show Detail

What is the new format of documentation comments in Swift 2? (XCode 7 beta 3)

There is this great article by Nate Cook and Mattt Thompson that describes the format of documentation comments in Swift. However, since Swift 2 in XCode 7 beta some parts of it do not seem to wor...

Show Detail

How to hide certain Objective-C method header documentation (HeaderDoc) comments

Is there a way in Objective-C documentation (HeaderDoc) to hide certain comments from being visible to the public? In other words, when you option-click (alt-click) on a method you're calling, tha...

Show Detail

How do I extract API documentation from a Swift file?

Swift has some ability to extract comments and documentation from a module. Automatically generate the Swift interface for a compiled module echo ":print_module CoreGraphics" | xcrun swift -depr...

Show Detail

Why the need to avoid C-style /*... */ comments in Swift?

I read in this Swift Style Guide, which is apparently based on Apple's own Swift library style: 'Non-documentation comments always use the double-slash format (//), never the C-style block format ...

Show Detail

Formatting tables in Swift documentation comments

I have a section of code like this sitting above my computed property: /** Data Types Conversions: ----------------------- +-----------------+-------------+ | kernel | Swift/Ob...

Show Detail

Is there any way to fold/collapse Rust documentation comments in Visual Studio Code?

In VSCode, is there any way to fold/collapse Rust documentation comments (i.e., newline comments which start with: //! and ///)? Swift has similar comments, so any answers pertaining to Swift may a...

Show Detail

Does Swift have documentation generation support?

Many languages support documentation comments to allow a generator (like javadoc or doxygen) to generate code documentation by parsing that same code. Does Swift have any type documentation comment

Show Detail