Rules which enforce generally accepted best practices.

ProhibitedInterfaceBuilder

Since: PMD 7.0.0

Priority: Medium High (2)

Creating views using Interface Builder should be avoided. Defining views by code allows the compiler to detect issues that otherwise will be runtime errors. It’s difficult to review the auto-generated code and allow concurrent modifications of those files. Consider building views programmatically.

This rule is defined by the following XPath expression:

//VariableDeclarationHead/Attributes/Attribute[AttributeName/Identifier/T-Identifier[@Text = "IBOutlet"]]
                    |
                    //FunctionHead/Attributes/Attribute[AttributeName/Identifier/T-Identifier[@Text = "IBAction"]]

Example(s):

class ViewController: UIViewController {
    @IBOutlet var label: UILabel! // violation, referencing a IBOutlet
}

class ViewController: UIViewController {
    var label: UILabel!
}

Use this rule by referencing it:

<rule ref="category/swift/bestpractices.xml/ProhibitedInterfaceBuilder" />

UnavailableFunction

Since: PMD 7.0.0

Priority: Medium (3)

Due to Objective-C and Swift interoperability some functions are often required to be implemented but aren’t really needed. It is extremely common that the sole implementation of the functions consist of throwing a fatal error. Marking these functions as unavailable prevents them from being executed while still making the compiler happy.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.swift.rule.bestpractices.UnavailableFunctionRule

Example(s):

required init?(coder _: NSCoder) { // violation, no unavailable attribute added to the function declaration
    fatalError("init(coder:) has not been implemented")
}

@available(*, unavailable)         // no violation
required init?(coder _: NSCoder) {
    fatalError("init(coder:) has not been implemented")

Use this rule by referencing it:

<rule ref="category/swift/bestpractices.xml/UnavailableFunction" />