Home > Software design >  Operator in variable Swift
Operator in variable Swift

Time:02-14

I am trying to make a function to see if all my textfields are filled in, I pass it an array, Let's say var array = ["1", "2", "3"]

And I want to check the if statement if array[0].isEmpty || array[1].isEmpty || array[2].isEmpty

But it has to be variable, so I thought about using a for loop and using the operator as a variable, that doesn't work, at least not how I am doing it, does anyone have an idea for this?

var op = false
    
    for field in array {
        op  = field.isEmpty
        op  = ||
    }
    
    if op {
        
    }

Kind regards Daan

CodePudding user response:

You could do the check as

array.first(where: \.isEmpty) != nil

As suggested by @MartinR in the comments you can use contains instead which makes it a bit neater and with the same performance

array.contains(where: \.isEmpty)

CodePudding user response:

Try

array.allSatisfy{ !$0.isEmpty } // false => means that one or more items is/are empty
  • Related