user54272 user54272 - 4 months ago 8
Swift Question

How to check nil string in if condition swift

So when I press the button I check if both the UITextField and the UITextView have some value inside them, but if they are nil then perform this.

So I tried this, but it didn't work:

@IBOutlet weak var defTextView = UITextView

@IBAction func btnTapped(sender: UIButton) {

if let definitionName = defTextView.text {

print(definitionName)

} else {

print("nil")

}
}


Instead of receiving the word "nil" I got empty Strings bring printed

Answer

try following code

if defTextView.text != nil || !(defTextView.text == "")  {

    print(definitionName)

} else {

    print("nil")

}
Comments