我想找出一個替代方法來做這樣的事情,使用範圍運算符。我可以在Swift的guard語句中使用範圍運算符嗎?
guard let statusCode = (response as? HTTPURLResponse)?.statusCode, statusCode >= 200 && statusCode <= 299 else {return}
也許是這樣的:
guard let statusCode = (response as? HTTPURLResponse)?.statusCode where (200...299).contains(statusCode) else {return}
或
guard let statusCode = (response as? HTTPURLResponse)?.statusCode, statusCode case 200...299 else {return}
這是可能的斯威夫特?
不錯的問題! – Fattie
相關:[我可以使用範圍運算符與if語句在Swift?](http://stackoverflow.com/questions/24893110/can-i-use-the-range-operator-with-if-statement-in- swift) –
switch語句中範圍的模式匹配由'〜='運算符定義。這是一個非常酷的功能,因爲這意味着您可以在switch語句中進行任何匹配,您可以使用'〜='手動執行。這也意味着您可以通過實現定製的'〜='操作符函數來擴展switch語句的功能。 – Alexander