如果声明在Swift中包含“或”

时间:2017-03-25 02:19:25

标签: swift if-statement

我正在编写一堆if语句:

if thing == "A" { do something}
if thing == "C" { do something}

如果我想要“A”和“C”做同样的事情,有没有办法说出来

if thing == "A" or "C" { do something }

我只是想清理一下而不是写出每一个实例。

谢谢!

3 个答案:

答案 0 :(得分:1)

我假设是斯威夫特。

你不能做任何事情:

if thing == "A" or "C" { do something }

但你可以这样做:

if thing == "A" || thing == "C" {
    do something
}

或者你可以这样做:

if ["A", "C"].contains(thing) {
    do something
}

或者你可以这样做:

switch thing {
case "A", "C":
    do something
default:
    break;
}

答案 1 :(得分:0)

斯威夫特:if thing == "A" || thing == "C" { do something }

答案 2 :(得分:0)

以上解决方案很棒,但我通常更喜欢swift提供的强大的Switch case语句,以更多的Swift模式。 将Thing定义为enum并执行以下操作:

<?php
$username = $_SESSION['username'];
$pdo = new PDO("mysql:host=127.0.0.1;dbname=acacs;port=8889", 'root', 'root');
$pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, TRUE);
$pdo->setAttribute( PDO::ATTR_EMULATE_PREPARES, FALSE);

$sql = "SELECT ItemID
FROM Item";
$stmt = $pdo->prepare($sql);
//Execute the statement.
$stmt->execute();
//Retrieve the rows using fetchAll.
$users = $stmt->fetchAll();
?>
<body>

<form method="POST">
<select id="dropdownItems" name="dropdownItemID">
    <?php foreach($users as $user): ?>
        <option value="<?= $user['ItemID']; ?>"><?= $user['ItemID']; ?></option>
    <?php endforeach; ?>
</select>
    <p>
        <input type="submit" value="Submit Item choice" name="item"/>
    </p>
</form>

<?php if(isset($_POST['dropdownItemID'] )){
    $sql2 = "SELECT SUM(DISTINCT (NumberOfUnits))
    FROM Item WHERE ItemID =  " . $_POST['dropdownItemID'];

    $stmt2 = $pdo->prepare($sql2);
//Execute the statement.
    $stmt2->execute();
//Retrieve the rows using fetchAll.
    $users2 = $stmt2->fetchColumn();
    #$var = var_dump($users5);
    $range = range(1, $users2);
    echo "<p>";
    echo "ItemID: " . $_POST['dropdownItemID'] . " Is Currently Selected";
    echo "</p>";

    echo "<form method=\"POST\">";
    echo "<select id=\"dropdownNumberOfItems\" name=\"dropdownItems\">";
    foreach ($range as $numberofitems) {
        echo "<option value='$numberofitems'>$numberofitems Items</option>";
    }
    echo "</select>";
        echo "<p>";
        echo "<input type=\"submit\" value=\"Submit Request\"/>";
    echo "</p>";
echo "</form>";

}
?>

<?php if(isset($_POST['dropdownItems'])) {
    $sql3 = "INSERT INTO Request(RequestID,
            UserName,
            TimeStamp,
            ServiceID,
            ItemID,
            ItemQuantity,
            ItemProvided,
            Status) VALUES (
            :RequestID, 
            :UserName, 
            :TimeStamp, 
            :ServiceID, 
            :ItemID,
            :ItemQuantity,
            :ItemProvided,
            :Status)";

    $stmt3 = $pdo->prepare($sql3);

    $stmt3->bindValue(':RequestID', NULL, PDO::PARAM_NULL);
    $stmt3->bindValue(':UserName', $username, PDO::PARAM_STR);
    $stmt3->bindValue(':TimeStamp', 1489874423, PDO::PARAM_INT);
// use PARAM_STR although a number
    $stmt3->bindValue(':ServiceID', 400003, PDO::PARAM_INT);
    $stmt3->bindParam(':ItemID', $$_POST['dropdownItemID'], PDO::PARAM_INT);
    $stmt3->bindParam(':ItemQuantity', $_POST['dropdownItems'], PDO::PARAM_INT);
    $stmt3->bindValue(':ItemProvided', 0, PDO::PARAM_INT);
    $stmt3->bindValue(':Status', 'pending', PDO::PARAM_STR);

    $stmt3->execute();
    $lastId = $pdo->lastInsertId();
}
?>