Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Swiftの ??の使い方について

かなり初歩的な質問です。
var sum = Int(textField1.text ?? "") ?? 0
こういった場合の??の詳しい説明を聞きたいです。
ネットで検索してもうまくヒットしませんでした。
検索の仕方でも構いません。
よろしくお願いします。

0

1Answer

記号の検索のコツは「Swift ハテナ 演算子」とか「Swift question operator」のように単語にして調べてみることです。

a ?? b は nil 結合演算子 (nil-coalescing operator) です。 Optional な値 a が nil でなければ a をアンラップして返し、 nil なら b を返します。つまり a != nil ? a! : b と同等です。

参考 https://docs.swift.org/swift-book/LanguageGuide/BasicOperators.html#ID72

1Like

Comments

  1. @Tatsuya_ss0121

    Questioner

    ありがとうございます!検索の仕方も参考にします!
    解説までありがとうございました!!

Your answer might help someone💌