rspec/rules/S4183/swift/rule.adoc

44 lines
909 B
Plaintext
Raw Normal View History

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++``
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
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
}
}
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
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)
include::message.adoc[]
include::highlighting.adoc[]
endif::env-github,rspecator-view[]