我可以在Swift的guard语句中使用范围运算符吗?

前端之家收集整理的这篇文章主要介绍了我可以在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}

这在Swift中可能吗?

随你心意:
guard
    let statusCode = (response as? HTTPURLResponse)?.statusCode,(200...299).contains(statusCode) else {return}

要么:

guard
    let statusCode = (response as? HTTPURLResponse)?.statusCode,case 200...299 = statusCode else {return}

要么:

guard
    let statusCode = (response as? HTTPURLResponse)?.statusCode,200...299 ~= statusCode else {return}
原文链接:https://www.f2er.com/swift/318633.html

猜你在找的Swift相关文章