【问题标题】:Switch in swift compiles but runs forever切换到 swift 编译但永远运行
【发布时间】:2017-06-23 15:23:29
【问题描述】:

我学习 swift atm 和 switch 语句是迄今为止最困难的部分。

下面的代码是为了简单地计算数组中的每一个投票,我希望它通过一个开关(!)来完成。

我的问题:

  1. 为什么操场一直跑? (重启了几次)
  2. 导入基础重要吗? (那行不是我的代码)

我更感兴趣的是它为什么不起作用以及它是如何工作的,而不是真正对正确的代码感兴趣。我很高兴有任何帮助!

    import UIKit
enum SchoolMascotOption {
    case salamander, marmot, neither
}
import Foundation
let mascotVotes: [SchoolMascotOption] = [.neither, .marmot, .salamander, .neither, .marmot, .neither, .neither, .marmot, .neither, .salamander, .salamander, .marmot, .neither, .neither, .salamander, .neither, .neither, .marmot, .salamander, .neither, .neither, .neither, .marmot, .marmot, .neither, .neither, .marmot, .salamander, .neither, .marmot, .marmot, .marmot, .marmot, .neither, .salamander, .salamander, .salamander, .salamander, .salamander, .salamander, .salamander, .marmot, .neither, .salamander, .salamander, .neither, .salamander, .neither, .salamander, .salamander, .salamander, .salamander, .salamander, .salamander, .marmot, .neither, .neither, .marmot, .salamander, .neither, .neither, .salamander, .salamander, .neither, .salamander, .salamander, .salamander, .salamander, .neither, .salamander, .neither, .salamander, .marmot, .salamander, .marmot, .salamander, .salamander, .marmot, .salamander, .neither, .marmot, .marmot, .marmot, .salamander, .marmot, .salamander, .marmot, .neither, .marmot, .neither, .salamander, .marmot, .marmot, .marmot, .neither, .marmot, .marmot, .salamander, .neither, .neither, .salamander, .neither, .neither, .marmot, .neither, .salamander, .salamander, .salamander, .neither, .neither, .salamander, .salamander, .salamander, .marmot, .salamander, .salamander, .marmot, .salamander, .neither, .marmot, .marmot, .neither, .neither, .salamander, .marmot, .neither, .marmot, .salamander, .salamander, .marmot, .salamander, .neither, .salamander, .marmot, .neither, .salamander, .marmot, .marmot, .salamander, .marmot, .salamander, .marmot, .salamander, .salamander, .marmot, .marmot, .neither, .marmot, .neither, .marmot, .salamander, .salamander, .salamander, .neither, .salamander, .salamander, .neither, .marmot, .neither, .marmot, .marmot, .marmot, .marmot, .neither, .marmot, .neither, .salamander, .marmot, .salamander, .neither, .salamander, .salamander, .marmot, .neither, .marmot, .neither, .salamander, .neither, .salamander, .neither, .neither, .marmot, .salamander, .neither, .marmot, .salamander, .marmot, .neither, .salamander, .neither, .neither, .salamander, .salamander, .salamander, .neither, .salamander, .neither, .marmot, .salamander, .marmot]

var salamanderCounts = 0
var marmotCounts = 0
var neitherCounts = 0

for vote in mascotVotes {

    switch vote {
    case .salamander :
        salamanderCounts += 1
    case .marmot :
        marmotCounts += 1
    case .neither :
        neitherCounts += 1
    }
}
print(salamanderCounts)
print(marmotCounts)
print(neitherCounts)

screenshot

谢谢大家帮忙!!!

【问题讨论】:

  • τ代码,您向我们展示了按预期工作,在我的计算机上打印 70、60、62 :-)。这里不需要导入 UIKit 甚至 Foundation ...
  • 谢谢,我试试看!也许我的 macbook 只是被这个巨大的任务压垮了,呵呵

标签: swift switch-statement swift-playground


【解决方案1】:

我想在操场上运行它不会仅仅因为数组的大小而闪电般快速。

import Foundation 在你 import UIKit 时是多余的,因为 UIKit 包括 Foundation(尽管在这个操场上两者都不需要)。

尝试从 Playground 中删除所有代码,等待 Playground 不再说“正在运行”或类似的话,然后将其粘贴回去。

如果这不能解决问题,请尝试将数组设置为五个元素,以确保长度不是问题。

祝你好运!

【讨论】:

  • 所以代码/逻辑本身有意义吗?非常感谢!!
  • @felixmp 没问题 :)
  • 我也有其他这么大的数组,并用 if 语句计算它们,我最多花了大约 10sek,为什么开关需要更长的时间?生病使数组更小,然后再试一次
猜你喜欢
  • 2016-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-30
相关资源
最近更新 更多