檢查集合是否包含值

var favoriteColors: Set = ["Red", "Blue", "Green"]
//favoriteColors = {"Blue", "Green", "Red"}

你可以使用 contains(_:) 方法檢查集合是否包含值。如果集合包含該值,它將返回 true。

if favoriteColors.contains("Blue") {
    print("Who doesn't like blue!")
}
// Prints "Who doesn't like blue!"