How to Compare One Value Against Multiple Values - Swift

Swift: How to correctly compare String value to multiple values

It's not really clear what these string are, but this is definitely the wrong data structure. I suspect you're looking for something like this, an array of stages that each contain an array of levels, which contain an array of strings.

struct Level {
var values: [String]
}

struct Stage {
var levels: [Level]
}

let stages = [
Stage(levels: [
Level(values: ["One", "Two"])
Level(values: ["Horse", "Shoe"]),
Level(values: ["One", "Two"]),
]),
Stage(levels: [
Level(values: ["Snow", "Sun"]),
Level(values: ["Smile", "Cry"]),
Level(values: ["Five", "Six"]),
]),
]

var currentStage = 1
var currentLogo = 2

// Remember that arrays are 0-indexed. If "currentStage" is 1-indexed
// you need to adjust it
let level = stages[currentStage - 1].levels[currentLogo - 1]
let words = level.values

if let text = textFieldChangedOut.text, words.contains(text) {
print("Contains the value")
}

What you're trying to do with dynamically computing the name of the variable is impossible in pure Swift. There are ways to achieve it by bridging to ObjC, but they're not the right way to attack this problem.

Compare multiple variables to the same expression

Use ranges!

if (1...5).contains(x) &&
(1...5).contains(y) &&
(1...5).contains(z) {

}

Alternatively, create a closure that checks whether something is in range:

let inRange: (Int) -> Bool = (1...5).contains
if inRange(x) && inRange(y) && inRange(z) {

}

As Hamish has suggested, the allSatisfy method in Swift 4.2 can be implemented as an extension like this:

extension Sequence {
func allSatisfy(_ predicate: (Element) throws -> Bool) rethrows -> Bool {
return try !contains { try !predicate($0) }
}
}

compare multiple arrays for same elements in swift

If your intent is to just check if a name occurs in more than one collection I think the best way to approach this is creating a single collection with all the names and filter the duplicates as shown in this post

let array1 = ["Max", "Peter","Kathrin", "Sara", "Kirsten", "Mike", "Elon"]
let array2 = ["Pamela", "Chris", "James", "Sebastian", "Mike"]
let array3 = ["John", "Daniel", "Susan", "Mathias", "Mike", "Donald"]
let array4 = ["Tim", "Kathrin", "Alan", "Chris", "Amy", "Sara"]
let array5 = ["Cara", "Charly", "Emily", "Maja", "Peter", "Sara"]

var names: [String] = []
names.append(contentsOf: array1)
names.append(contentsOf: array2)
names.append(contentsOf: array3)
names.append(contentsOf: array4)
names.append(contentsOf: array5)


extension RangeReplaceableCollection where Element: Hashable {
var duplicates: Self {
var set: Set<Element> = []
var filtered: Set<Element> = []
return filter { !set.insert($0).inserted && filtered.insert($0).inserted }
}
}

// Output should be: Peter, Kathrin, Mike, Sara, Chris
print(names.duplicates) // ["Mike", "Kathrin", "Chris", "Sara", "Peter"]

Compare three values for equality

You can use the power of tuples and the Transitive Property of Equality.

if (number1, number2) == (number2, number3) {

}

The clause of this IF is true only when number1 is equals to number2 AND number2 is equals to number3. It means that the 3 values must be equals.

How to compare a value to several other values in one line in Kotlin

I think the simplest way is with the in operator:

a in listOf(b, c)

you can include as many items as you want inside the list.

Multiple value checking with swifts guard statement?

Guard statements aren't the right thing to use here. If any of them fail, then you will return from the function and your later statements won't get executed. So if myAmount doesn't have a valid value in it, you'll never correct myRate and myFee.

if Double(myAmount.text ?? "") == nil {
myAmount.text = "0.00"
}

This pattern would suit you better:

  • the text value isn't force unwrapped any more, which will prevent one potential crash - ?? is the nil coalescing operator which will use either the text value or, in this case, an empty string
  • there is no return so the function will check all of the values.


Related Topics



Leave a reply



Submit