rspec/rules/S4187/swift/rule.adoc

53 lines
1.3 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
Classes should only hold ``++weak++`` references to delegate fields with ``++class++`` type. Otherwise, the owning class will have a ``++strong++`` reference to its delegate, and vice versa, and the OS won't be able to deallocate either of them..
Note that this only applies to non-computed delegate fields in classes, and not to fields in ``++struct++``s and ``++enum++``s.
=== 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 {
var delegate: ConventionDelegate? // 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 {
weak var delegate: ConventionDelegate?
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Mark this variable as "weak" to avoid reference cycles.
=== Highlighting
variable declaration
'''
== Comments And Links
(visible only on this page)
=== on 5 Sep 2017, 17:57:58 Elena Vilchik wrote:
Problem of reference cycles is an important one in Swift (see this https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html[doc]), but in order to track this pattern we need a semantic information, which we don't have.
endif::env-github,rspecator-view[]