
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
43 lines
1.1 KiB
Plaintext
43 lines
1.1 KiB
Plaintext
== Why is this an issue?
|
|
|
|
The point of using an optional is to signal that the value may be ``++nil++`` and to provide graceful ways of dealing with it if it is ``++nil++``. While implicitly unwrapped optionals still provide means of dealing with ``++nil++`` values, they also signal that the value won't be ``++nil++``, and unwrap it automatically. In addition to sending a decidedly mixed signal, this could lead to runtime errors if the value ever is ``++nil++``.
|
|
|
|
|
|
It is safest, and clearest to use either an optional or a plain type and avoid the boggy middle ground of implicitly unwrapped optionals.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,swift]
|
|
----
|
|
var greeting : String! // Noncompliant
|
|
|
|
println(greeting) // At this point the value is nil. Runtime error results
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,swift]
|
|
----
|
|
var greeting : String?
|
|
|
|
if let howdy = greeting {
|
|
println(howdy)
|
|
}
|
|
----
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Convert this implicitly unwrapped optional to a plain optional.
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|