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

时间:2017-05-14 18:02:24

标签: swift

我正试图找出另一种方法来做这样的事情,使用范围运算符。

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中使用吗?

3 个答案:

答案 0 :(得分:5)

如你所愿:

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}

答案 1 :(得分:3)

这是一个可能的解决方案

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

答案 2 :(得分:1)

只是针对不同的解决方案,您还可以使用:

@echo off
title Network Folder Check

pushd "%~dp0"
:00
cls

for /f "delims=\\" %%A in (Files-to-Check.txt) do set Server=%%A
    setlocal EnableDelayedExpansion
        ping -n 1 %Server% | findstr TTL= >nul 
            if %errorlevel%==1 ( 
                ping -n 1 %Server% | findstr "Reply from" | findstr "time" >nul
                    if !errorlevel!==1 (echo Network Asset %Server% Not Found & pause & goto EOF)
            )
:EOF