Check Multiple string is not nil and not empty in swift.

You can add all Strings to an Array and call allSatisfy on that array.

func isSetupDone(token1: String?, token2: String?, token3: String?) -> Bool {
    let tokens = [token1, token2, token3]
    return tokens.allSatisfy { $0 != nil && $0?.isEmpty == false }
}
You can also merge the two conditions into a single one by optional chaining the optional strings, since $0?.isEmpty == false will evaluate to false in case $0 is nil.

func isSetupDone(token1: String?, token2: String?, token3: String?) -> Bool {
    [token1, token2, token3].allSatisfy {$0?.isEmpty == false }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s