skip to Main Content

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

2

Answers


  1. 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)
    
    Login or Signup to reply.
  2. Try

    array.allSatisfy{ !$0.isEmpty } // false => means that one or more items is/are empty
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search