rspec/rules/S4183/swift/rule.adoc

52 lines
989 B
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
Classes can register themselves to receive notifications using ``++NotificationCenter.add++``. Having done so, it seems suspicious that a class would opt to stop receiving notifications before de-initialization. For that reason, this rule raises an issue when ``++NotificationCenter.default.removeObserver(self)++`` is called anywhere but in ``++deinit++``
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,swift]
2021-04-28 16:49:39 +02:00
----
class MyClass {
func doTheThing() {
//...
NotificationCenter.default.removeObserver(self) // Noncompliant
}
}
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,swift]
2021-04-28 16:49:39 +02:00
----
class MyClass {
func doTheThing() {
//...
}
func deinit() {
NotificationCenter.default.removeObserver(self)
}
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Move this call to (a|the) "deinit" method.
=== Highlighting
``++removeObserver(self)++``
endif::env-github,rspecator-view[]