Redundant @objc Attribute

Objective-C attribute (@objc) is redundant in declaration

  • Identifier: redundant_objc_attribute
  • Enabled by default: Yes
  • Supports autocorrection: Yes
  • Kind: idiomatic
  • Analyzer rule: No
  • Minimum Swift compiler version: 5.0.0
  • Default configuration:
    KeyValue
    severity warning

Non Triggering Examples

@objc private var foo: String? {}
@IBInspectable private var foo: String? {}
@objc private func foo(_ sender: Any) {}
@IBAction private func foo(_ sender: Any) {}
@GKInspectable private var foo: String! {}
private @GKInspectable var foo: String! {}
@NSManaged var foo: String!
@objc @NSCopying var foo: String!
@objcMembers
class Foo {
    var bar: Any?
    @objc
    class Bar: NSObject {
        @objc
        var foo: Any?
    }
}
@objc
extension Foo {
    var bar: Int {
        return 0
    }
}
extension Foo {
    @objc
    var bar: Int { return 0 }
}
@objc @IBDesignable
extension Foo {
    var bar: Int { return 0 }
}
@IBDesignable
extension Foo {
    @objc
    var bar: Int { return 0 }
    var fooBar: Int { return 1 }
}
@objcMembers
class Foo: NSObject {
    @objc
    private var bar: Int {
        return 0
    }
}
@objcMembers
class Foo {
    @objc
    class Bar: NSObject {
        @objc var foo: Any?
    }
}
@objcMembers
class Foo: NSObject {
    @objc class Bar {}
}
extension BlockEditorSettings {
    @objc(addElementsObject:)
    @NSManaged public func addToElements(_ value: BlockEditorSettingElement)
}
@objcMembers
public class Foo: NSObject {
    @objc
    private func handler(_ notification: Notification) {
    }

    func registerForNotifications() {
        NotificationCenter.default.addObserver(self, selector: #selector(handler(_:)), name: nil, object: nil)
    }
}
class Foo: NSObject { }

@objc extension Foo {
    @objc enum Bar: Int {
       case bar
    }

    var bar: Bar { .bar }
}
class Foo: NSObject { }

@objc extension Foo {
    @objc private enum Baz: Int {
      case baz
    }

    private var baz: Baz { .baz }
}
@objcMembers
internal class Foo: NSObject {
    @objc
    private var baz: Int = 1

    var x: Any? {
        value(forKey: "baz")
    }
}
@objcMembers
class Foo: NSObject {
    @objc enum Bar: Int {
       case bar
    }
}

Triggering Examples

@objc @IBInspectable private var foo: String? {}
@IBInspectable @objc private var foo: String? {}
@objc @IBAction private func foo(_ sender: Any) {}
@IBAction @objc private func foo(_ sender: Any) {}
@objc @GKInspectable private var foo: String! {}
@GKInspectable @objc private var foo: String! {}
@objc @NSManaged private var foo: String!
@NSManaged @objc private var foo: String!
@objc @IBDesignable class Foo {}
@objcMembers
class Foo: NSObject {
    @objc var bar: Any?
}
@objcMembers
class Foo: NSObject {
    @objc var bar: Any?
    @objc var foo: Any?
    @objc
    class Bar {
        @objc
        var foo: Any?
    }
}
@objc
extension Foo {
    @objc
    var bar: Int {
        return 0
    }
}
@objc @IBDesignable
extension Foo {
    @objc
    var bar: Int {
        return 0
    }
}
@objcMembers
class Foo: NSObject {
    @objcMembers
    class Bar: NSObject {
        @objc var foo: Any
    }
}
@objc
extension Foo {
    @objc
    private var bar: Int {
        return 0
    }
}