Swift检查集合是否包含值

示例

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!"