Swift: If let statement failing to handle empty array
我有一个使用 Foursquare API 下载 JSON 数据的应用程序。我正在使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | if let response: NSDictionary = data["response"] as? [String: AnyObject], groups: NSArray = response["groups"] as? NSArray, // Error here \\|/ (sometimes groups array is empty) dic: NSDictionary = groups[0] as? NSDictionary, items: NSArray = dic["items"] as! NSArray { } else { // Never gets here. Why? // IF the groups array is empty, then the if let should return // false and drop down to the else block, right? } |
我对 Swift 比较陌生,谁能告诉我为什么会发生这种情况以及我能做些什么来解决这个问题?谢谢
如果数组为空,则必须在
空数组永远不是可选的
1 2 3 4 5 6 7 8 9 | if let response = data["response"] as? [String: AnyObject], groups = response["groups"] as? NSArray { if !groups.isEmpty { if let dic = groups[0] as? NSDictionary { items = dic["items"] as! NSArray // do something with items println(items) } } } else ... |
向下转换类型时可以省略所有类型注释
但是,您可以使用 where 子句执行检查,这适用于 Swift 1.2 和 2
1 2 3 4 5 6 7 | if let response = data["response"] as? [String: AnyObject], groups = response["groups"] as? [AnyObject] where !groups.isEmpty, let dic = groups[0] as? NSDictionary, items = dic["items"] as? NSArray { // do something with items println(items) } else {... |
在尝试访问之前确保数组不为空:
1 2 3 4 5 6 7 8 9 10 11 | if let response: NSDictionary = data["response"] as? [String: AnyObject], let groups: NSArray = response["groups"] as? NSArray where groups.count > 0 { if let dic: NSDictionary = groups[0] as? NSDictionary, let items: NSArray = dic["items"] as? NSArray { // Do something.. return // When the 2nd if fails, we never hit the else clause } } // Do logic for else here ... |
在语句之间使用
例如这不会崩溃:
1 2 3 4 5 6 7 | var a = 4; var c = 5; var array = Array<Int>(); if a > 2 && c > 10 && array[0] != 0 { } |