[swift-users] Optional chaining and String properties
Rimantas Liubertas
rimantas at gmail.com
Mon Aug 1 12:52:20 CDT 2016
>
> var s: String? = "Foo"
> print(s?.characters)
>
> The result indicates that s?.characters is indeed an Optional instance, indicating that s?.characters.count should be illegal.
>
> Why is s?.characters.count a legal expression?
See print(s?.characters.count) — you get the optional despite count not being defined as optional
Also, try this:
struct Foo {
let bar: Int
}
var foo: Foo? = Foo(bar: 42)
print(foo?.bar)
Then try this:
struct Foo {
let bar: Int
let baz: Int?
}
var foo: Foo? = Foo(bar: 42, baz: 69)
print(foo?.bar)
print(foo?.baz?)
This may give you some ideas.
Best regards,
Rimantas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160801/c5b975e5/attachment.html>
More information about the swift-users
mailing list