Can DocC Generate Documentation for Extensions?

Apple Recommended

Replies

I have the same question too.

Add a Comment

The solution is to use inside a package a public protocol with a type constraint and declare the type to conform to the protocol. Now you get the documentation you want.

It is a cleaner solution than just using an extension.

Example:

import Foundation

// The extension:
public protocol FileManagerExtension: FileManager { }
extension FileManager: FileManagerExtension { } // I don't know why this doesn't have to be public!

// The new function:
public extension FileManagerExtension {
    /// My new function.
    /// - Returns: 42.
    func myNewFuntion() -> Int { return 42 }
}
  • I tried Nankea's solution, but it doesn't work for SwiftUI's View. I get "Extension of protocol 'View' cannot have an inheritance clause" on the second line.

Add a Comment

I tried Nankea's solution, but it doesn't work for SwiftUI's View:

public protocol ViewExtension: View { }
extension View: ViewExtension { } // I don't know why this doesn't have to be public!

public extension ViewExtension {
    /// Places an alphabetical index next to the view.
    /// - Parameter style: A style to apply to the index.
    /// - Returns: The view with the added index.
    public func alphabeticalIndex(style: AlphabeticalIndexStyle = AlphabeticalIndexStyle.default) -> some View {
        self.modifier(AlphabeticalIndex(style: style))
    }
}

I get "Extension of protocol 'View' cannot have an inheritance clause" on the second line.

This REALLY needs to be fixed!

  • You cannot define an extension to View because it is a protocol.

Add a Comment

Nankea, while you have a good point regarding the extension of unowned types, DocC still doesn't generate documentation for the extensions.