<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## `takeIf` 與 `takeUnless` 除了作用域函數外,標準庫還包含函數 `takeIf` 及 `takeUnless`。這倆函數使你可以將對象狀態檢查嵌入到調用鏈中。 當以提供的謂詞在對象上進行調用時,若該對象與謂詞匹配,則 `takeIf` 返回此對象。否則返回 `null`。因此,`takeIf` 是單個對象的過濾函數。反之,`takeUnless`如果不匹配謂詞,則返回對象,如果匹配則返回 `null`。該對象作為 lambda 表達式參數(`it`)來訪問。 ```kotlin import kotlin.random.* fun main() { //sampleStart val number = Random.nextInt(100) val evenOrNull = number.takeIf { it % 2 == 0 } val oddOrNull = number.takeUnless { it % 2 == 0 } println("even: $evenOrNull, odd: $oddOrNull") //sampleEnd } ``` 當在 `takeIf` 及 `takeUnless` 之后鏈式調用其他函數,不要忘記執行空檢查或安全調用(`?.`),因為他們的返回值是可為空的。 ```kotlin fun main() { //sampleStart val str = "Hello" val caps = str.takeIf { it.isNotEmpty() }?.toUpperCase() //val caps = str.takeIf { it.isNotEmpty() }.toUpperCase() // 編譯錯誤 println(caps) //sampleEnd } ``` `takeIf` 及 `takeUnless` 與作用域函數一起特別有用。 一個很好的例子是用 `let` 鏈接它們,以便在與給定謂詞匹配的對象上運行代碼塊。 為此,請在對象上調用 `takeIf`,然后通過安全調用(`?.`)調用 `let`。對于與謂詞不匹配的對象,`takeIf` 返回 `null`,并且不調用 `let`。 ```kotlin fun main() { //sampleStart fun displaySubstringPosition(input: String, sub: String) { input.indexOf(sub).takeIf { it >= 0 }?.let { println("The substring $sub is found in $input.") println("Its start position is $it.") } } displaySubstringPosition("010000011", "11") displaySubstringPosition("010000011", "12") //sampleEnd } ``` 沒有標準庫函數時,相同的函數看起來是這樣的: ```kotlin fun main() { //sampleStart fun displaySubstringPosition(input: String, sub: String) { val index = input.indexOf(sub) if (index >= 0) { println("The substring $sub is found in $input.") println("Its start position is $index.") } } displaySubstringPosition("010000011", "11") displaySubstringPosition("010000011", "12") //sampleEnd } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看