Check Empty String in Swift

Check empty string in Swift?

There is now the built in ability to detect empty string with .isEmpty:

if emptyString.isEmpty {
print("Nothing to see here")

Apple Pre-release documentation: "Strings and Characters".

How to check Multiple string is not nil and not empty in swift 5?

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 }

Swift - check if attributed text is empty

NSAttributedText has no isEmpty property but it has one called length. You can simply check if it is equal to zero:

if currentValue.length == .zero {
// Perform logic

Swift - 2 empty string arrays

if employees.isEmpty == false {
print("This shouldnt be called 1")
OneSignal.postNotification(["contents": ["en": "\(message)"],
"include_player_ids": employees,
"headings": ["en": "Hey \(businessName)"],
"ios_badgeType": "Increase",
"ios_badgeCount": 1])
} else {
print("Empty no execution")

print("Checking inside employees here")
print("Checking inside uids here")}

if uids.isEmpty == false {
print("This shouldnt be called 2")
let pathing = ref.child("notification").child(uids).childByAutoId()
} else {
print("Empty no execution")

Try the above code. I have moved the part where you show the array in else so that if array is nil it will show.

Use String isEmpty to check for empty string

The empty string is the only empty string, so there should be no cases where string.isEmpty() does not return the same value as string == "". They may do so in different amounts of time and memory, of course. Whether they use different amounts of time and memory is an implementation detail not described, but isEmpty is the preferred way to check in Swift (as documented).

How to check nil string in if condition swift

defTextView.text is empty String "" instead of nil.

Try where clause to check if it is empty:

@IBOutlet weak var defTextView = UITextView

@IBAction func btnTapped(sender: UIButton) {

if let definitionName = defTextView.text where !definitionName.isEmpty {


} else {



Related Topics

Leave a reply