44 lines
909 B
Plaintext
44 lines
909 B
Plaintext
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
|
|
|
|
[source,swift]
|
|
----
|
|
class MyClass {
|
|
func doTheThing() {
|
|
//...
|
|
NotificationCenter.default.removeObserver(self) // Noncompliant
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
[source,swift]
|
|
----
|
|
class MyClass {
|
|
func doTheThing() {
|
|
//...
|
|
}
|
|
|
|
func deinit() {
|
|
NotificationCenter.default.removeObserver(self)
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::message.adoc[]
|
|
|
|
include::highlighting.adoc[]
|
|
|
|
endif::env-github,rspecator-view[]
|