我想找出一个替代方法来做这样的事情,使用范围运算符。我可以在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