【问题标题】:how to use array.count in swift for-in loop如何在快速 for-in 循环中使用 array.count
【发布时间】:2021-05-02 18:25:15
【问题描述】:

我是 swift 的初学者(可能),我正在学习如何使用数组我试图创建一个循环量为 1...array.count 的 for-in 循环,但我得到了一个错误的:

致命错误:索引超出范围当前堆栈跟踪:0
libswiftCore.so 0x00007f0f71f0aea0 swift_reportError + 50 1 libswiftCore.so
0x00007f0f71f7c0c0 swift_stdlib_reportFatalError + 69 2
libswiftCore.so 0x00007f0f71e775d7 + 3347927 3 libswiftCore.so 0x00007f0f71c94d80 fatalErrorMessage(:
:file:line:flags:) + 19 4 libswiftSwiftOnoneSupport.so 0x00007f0f755c7ad0 专用 Array.subscript.getter + 85 6 快速
0x00000000004f23c9 + 992201 7 迅速
0x00000000004f6a40 + 1010240 8 迅速
0x00000000004e62ef + 942831 9 迅速
0x00000000004d5093 + 872595 10 迅速
0x00000000004d0e4e + 855630 11 迅速
0x0000000000473c16 + 474134 12 libc.so.6
0x00007f0f73771ab0 __libc_start_main + 231 13 迅速
0x000000000047387a + 473210 堆栈转储: 0. 程序参数:/usr/bin/swift -frontend -interpret Forecast.swift -disable-objc-interop -module-name Forecast /usr/bin/swift[0x4521834] /usr/bin/swift[0x451f48e] /usr/bin/swift[0x4521c48] /lib/x86_64-linux-gnu/libpthread.so.0(+0x128a0)[0x7f0f7532b8a0] /usr/lib/swift/linux/libswiftCore.so(+0x3315d7)[0x7f0f71e775d7] /usr/lib/swift/linux/libswiftCore.so($ss18_fatalErrorMessage__4file4line5flagss5NeverOs12StaticStringV_A2HSus6UInt32VtF+0x13)[0x7f0f71c94d93] /usr/lib/swift/linux/libswiftSwiftOnoneSupport.so($sSayxSicigSi_Tg5+0x55)[0x7f0f755c7b25] [0x7f0f7575d315] /usr/bin/swift[0x4f23c9] /usr/bin/swift[0x4f6a40] /usr/bin/swift[0x4e62ef] /usr/bin/swift[0x4d5093] /usr/bin/swift[0x4d0e4e] /usr/bin/swift[0x473c16] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x7f0f73771b97] /usr/bin/swift[0x47387a]

我该怎么办?这是我的代码,用于 CodeAcademy:

var temperature: [Int] = [66, 68, 72, 76, 80, 82, 85, 85, 84, 82, 81, 78, 74, 73, 72, 71, 70, 69, 68, 65, 63, 62, 61, 63]

// Write your code below ????
for i in 1...temperature.count{
  print(temperature[i])
}

【问题讨论】:

  • 首先分享你的代码 :)
  • 我将代码添加到问题中

标签: arrays swift loops fatal-error


【解决方案1】:

我认为您的目标是打印所有 temperature 元素。改变

for i in 1...temperature.count {

for i in 0..<temperature.count {

数组索引从零开始,到计数小于一个结束。 ..&lt; 运算符很好地处理了这个问题。或者,更好的是,说

for i in temperature.indices {

【讨论】:

  • 谢谢!我尝试了 temperature.indices,现在我得到了 66 68 72 76 80 82 85 85 84 82 81 78 74 73 72 71 70 69 68 65 63 62 61 63
  • @TechZacharyZN 您应该始终使用集合索引。你会明白为什么在迭代集合的一部分时它如此重要
【解决方案2】:

另一种选择是使用forEach

array.forEach { print($0) }

【讨论】:

    【解决方案3】:

    使用for i in temperature。这是基本的数组迭代,请记住:D

    【讨论】:

      【解决方案4】:
      var temperature: [Int] = [66, 68, 72, 76, 80, 82, 85, 85, 84, 82, 81, 78, 74, 73, 72, 71, 70, 69, 68, 65, 63, 62, 61, 63]
      
      // Write your code below ?
      for i in 1...temperature.count{
        print(temperature[i-1])
      }
      

      您收到此错误是因为temperature[temperature.count] 不存在。您需要将它限制到temperature[temperature.count-1] 并且索引应该从 1 而不是 0 开始。

      或者

      for i in 0...(temperature.count-1){
        print(temperature[i])
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-13
        • 1970-01-01
        • 2017-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多