<i id='qlakF'><tr id='qlakF'><dt id='qlakF'><q id='qlakF'><span id='qlakF'><b id='qlakF'><form id='qlakF'><ins id='qlakF'></ins><ul id='qlakF'></ul><sub id='qlakF'></sub></form><legend id='qlakF'></legend><bdo id='qlakF'><pre id='qlakF'><center id='qlakF'></center></pre></bdo></b><th id='qlakF'></th></span></q></dt></tr></i><div id='qlakF'><tfoot id='qlakF'></tfoot><dl id='qlakF'><fieldset id='qlakF'></fieldset></dl></div>
<legend id='qlakF'><style id='qlakF'><dir id='qlakF'><q id='qlakF'></q></dir></style></legend>

<tfoot id='qlakF'></tfoot>

      • <bdo id='qlakF'></bdo><ul id='qlakF'></ul>
      1. <small id='qlakF'></small><noframes id='qlakF'>

      2. 如何在 Swift 的布尔数组中找到多个 True 语句

        时间:2023-10-05
          <i id='bNF1m'><tr id='bNF1m'><dt id='bNF1m'><q id='bNF1m'><span id='bNF1m'><b id='bNF1m'><form id='bNF1m'><ins id='bNF1m'></ins><ul id='bNF1m'></ul><sub id='bNF1m'></sub></form><legend id='bNF1m'></legend><bdo id='bNF1m'><pre id='bNF1m'><center id='bNF1m'></center></pre></bdo></b><th id='bNF1m'></th></span></q></dt></tr></i><div id='bNF1m'><tfoot id='bNF1m'></tfoot><dl id='bNF1m'><fieldset id='bNF1m'></fieldset></dl></div>
          <tfoot id='bNF1m'></tfoot>
            <tbody id='bNF1m'></tbody>

              <legend id='bNF1m'><style id='bNF1m'><dir id='bNF1m'><q id='bNF1m'></q></dir></style></legend>
              1. <small id='bNF1m'></small><noframes id='bNF1m'>

                • <bdo id='bNF1m'></bdo><ul id='bNF1m'></ul>
                  本文介绍了如何在 Swift 的布尔数组中找到多个 True 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是一名新开发人员,似乎无法弄清楚如何在布尔数组中找到 True 语句的数量.我知道如何按索引而不是按值查找.任何帮助将不胜感激.

                  I am a new developer and cannot seem to figure out how to find the number of True statements in an Array of Booleans. I know how to find by index but not by value. Any assistance would be appreciated.

                  let arrayElement = [Bool](repeating: false, count: 10)
                  var before: [[Bool]] = [[Bool]](repeating: arrayElement, count:10)
                  for i in 0 ..< 10 {
                      for j in 0 ..< 10 {
                          if arc4random_uniform(3) == 1 {
                              before[i][j] = true  
                          }        
                      }
                  }
                  

                  推荐答案

                  计算一维数组中true条目数的方法

                  一种方法是过滤您的 Bool 元素数组(对于 true)并简单地计算过滤后数组中剩余元素的数量

                  Methods for counting number of true entries in a 1D array

                  One method would be to filter your array of Bool elements (for true) and simply count the number of remaining elements in the filtered array

                  let arr = [false, true, true, false, true]
                  let numberOfTrue = arr.filter{$0}.count
                  print(numberOfTrue) // 3
                  

                  另一种方法是reduce(展开)数组并为每个等于true

                  Another approach is to reduce (unfold) the array and increment a counter for each element that equals true

                  let arr = [false, true, true, false, true]
                  let numberOfTrue = arr.reduce(0) { $0 + ($1 ? 1 : 0) }
                  print(numberOfTrue) // 3
                  

                  或者,传统的 for 循环(带有条件循环签名)方法,与 reduce 方法相当:

                  Or, a traditional for loop (with conditional in loop signature) approach, comparable top the reduce method:

                  let arr = [false, true, true, false, true]
                  var trueCounter = 0
                  for bElem in arr where bElem { trueCounter += 1 }
                  print(trueCounter) // 3
                  

                  应用于你的例子:使用joined()实现一维数组

                  通过简单地应用 .joined()[[Bool]] 数组上依次构造一个 [Bool] 数组.

                  Applied to your example: use joined() to achieve a 1D array

                  The methods above can easily be applied to an array of arrays (of Bool elements: type [[Bool]]) by simply applying .joined() on the [[Bool]] array to sequentially construct a [Bool] array.

                  /* 'before' is of type [[Bool]], constructed as described
                     in the question */
                  let numberOfTrueAlt1 = before.joined().filter{$0}.count
                  
                  let numberOfTrueAlt2 = before.joined().reduce(0) { $0 + ($1 ? 1 : 0) }
                  
                  var numberOfTrueAlt3 = 0
                  for bElem in before.joined() where bElem { numberOfTrueAlt3 += 1 }
                  

                  这篇关于如何在 Swift 的布尔数组中找到多个 True 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Objective-c 中,比较 2 个 BOOL 值的安全和好方法? 下一篇:处理这个布尔值的正确方法是什么?

                  相关文章

                  1. <legend id='QDahI'><style id='QDahI'><dir id='QDahI'><q id='QDahI'></q></dir></style></legend>

                      • <bdo id='QDahI'></bdo><ul id='QDahI'></ul>
                      <tfoot id='QDahI'></tfoot>
                    1. <i id='QDahI'><tr id='QDahI'><dt id='QDahI'><q id='QDahI'><span id='QDahI'><b id='QDahI'><form id='QDahI'><ins id='QDahI'></ins><ul id='QDahI'></ul><sub id='QDahI'></sub></form><legend id='QDahI'></legend><bdo id='QDahI'><pre id='QDahI'><center id='QDahI'></center></pre></bdo></b><th id='QDahI'></th></span></q></dt></tr></i><div id='QDahI'><tfoot id='QDahI'></tfoot><dl id='QDahI'><fieldset id='QDahI'></fieldset></dl></div>

                      <small id='QDahI'></small><noframes id='QDahI'>