[swift-users] Optional chaining and String properties

Saagar Jha saagar at saagarjha.com
Mon Aug 1 13:25:44 CDT 2016


When you write `(s?.characters).count`, the parentheses are evaluated first; `(s?.characters)` gives an `String.CharacterView?`. Accessing the `String.CharacterView?`’s `count` property requires a `?`: `(s?.characters)?.count`. `s?.characters.count`, on the other hand, is applying chaining, which only gives an Optional at the end, intermediate properties don’t require a `?` unless they’re Optional themselves.

Saagar Jha



> On Aug 1, 2016, at 11:17, Stephen Schaub <sschaub at gmail.com> wrote:
> 
> I understand that the String.characters property is not optional. However, I am puzzled as to why
> 
> s?.characters.count
> 
> is legal, but
> 
> (s?.characters).count
> 
> is not. This seems counterintuitive. Can someone explain the logic or rules being used here?
> 
> Stephen
> 
> 
> 
> 
> On Mon, Aug 1, 2016 at 2:09 PM, Saagar Jha <saagar at saagarjha.com <mailto:saagar at saagarjha.com>> wrote:
> 
> Saagar Jha
> 
> This isn’t quite how optional chaining in Swift works; see the Swift Programming Guide <https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/OptionalChaining.html>, specifically “Linking Multiple Levels of Chaining". Basically, `s?.characters.count` works because `s.characters` isn’t Optional. You only use ? on properties that are Optional.
> 
>> On Aug 1, 2016, at 10:26, Stephen Schaub via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>> 
>> With optional chaining, if I have a Swift variable
>> 
>>     var s: String?
>> 
>> s might contain nil, or a String wrapped in an Optional. So, I tried this to get its length:
>> 
>>     let count = s?.characters?.count ?? 0
>> 
>> However, the compiler wants this:
>> 
>>     let count = s?.characters.count ?? 0
>> 
>> or this:
>> 
>>     let count = (s?.characters)?.count ?? 0
>> 
>> My understanding of optional chaining is that, once you start using '?.' in a dotted expression, the rest of the properties evaluate as optional and are typically accessed by '?.', not '.'.
>> 
>> So, I dug a little further and tried this in the playground:
>> 
>> 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?
>> 
>> 
>> --
>> Stephen Schaub
>> _______________________________________________
>> swift-users mailing list
>> swift-users at swift.org <mailto:swift-users at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-users <https://lists.swift.org/mailman/listinfo/swift-users>
> 
> 
> 
> 
> -- 
> Stephen Schaub

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160801/34ecd782/attachment.html>


More information about the swift-users mailing list