How to check if a string is nil (null) or empty in Swift

1 Answer

0 votes
import Foundation

func isStringNilOrEmpty(_ str: String?) -> Bool {
    if let str = str {
        return str.isEmpty
    }
    
    return true
}

var s: String? = nil
print(isStringNilOrEmpty(s)) 

s = ""
print(isStringNilOrEmpty(s)) 

s = "Swift Programming"
print(isStringNilOrEmpty(s)) 
 
 
 
/*
run:
   
true
true
false
 
*/

 



answered Nov 6, 2024 by avibootz

Related questions

...