Notification Center Detachment

An object should only remove itself as an observer in deinit

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

Non Triggering Examples

class Foo {
   deinit {
       NotificationCenter.default.removeObserver(self)
   }
}
class Foo {
   func bar() {
       NotificationCenter.default.removeObserver(otherObject)
   }
}

Triggering Examples

class Foo {
   func bar() {
       NotificationCenter.default.removeObserver(self)
   }
}