如何在swich case(swift)中创建循环?

时间:2015-04-30 18:25:49

标签: ios xcode swift loops boolean

我写了那段代码,但是我无法在错误的情况下制作循环。如何为错误条件制作循环?

注意:“isConnectedToNetwork()”返回一个布尔表达式。

switch Reachability.isConnectedToNetwork() {
    case false :
        println("Internet connection FAILED")
        var alert = UIAlertView(title: "No Internet Connection", message: "Make sure your device is connected to the internet.", delegate: nil, cancelButtonTitle: "OK")
        alert.show()
        fallthrough
     default:
         println(“Internet connection OK")
}  

1 个答案:

答案 0 :(得分:1)

while !Reachability.isConnectedToNetwork() {
   println("Internet connection FAILED")
   var alert = UIAlertView(title: "No Internet Connection", message: "Make sure your device is connected to the internet.", delegate: nil, cancelButtonTitle: "OK")
   alert.show()
}
println("Internet connection OK")

虽然这回答了你的问题,但这是一个非常糟糕的主意。请不要在任何应用程序中实际使用它,而是需要按照@i_am_jorf的建议实现reachability callbacks,或重新考虑此代码的设计