[swift-users] ?? operator question

zh ao owenzx at gmail.com
Mon Apr 18 08:23:52 CDT 2016


I do think there is something wrong here.

// Xcode 7.3.1, Swift 2.2

let t1: Int? = 2 // struct Int?
let y = t1 ?? "abcdf" // error
let x = t1 ?? NSFont(name: "", size: 0) // x: NSObject?

for x, how could it be NSObject?, as t1 is a struct?

zhaoxin

On Mon, Apr 18, 2016 at 6:59 PM, Marco Feltmann via swift-users <
swift-users at swift.org> wrote:

> Am 18.04.2016 um 11:51 schrieb tuuranton--- via swift-users:
>
> Why does this compile?
> let t1: Int? = 2print(t1 ?? "asdf")
>
> Well, I guess you try to re-typedef (??) a language construct.
> According to NSHipster ?? is the languages nil coalescing operator.
>
> So it simply compiles because t1 isn't nil.
>
> Further reference:
> https://en.wikipedia.org/wiki/Null_coalescing_operator#Swift
>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160418/840f10e1/attachment.html>


More information about the swift-users mailing list