close

Check if textfield is not empty and text is not only spaces in swift

QuestionsCategory: iOS (iPhone, iPad)Check if textfield is not empty and text is not only spaces in swift
Patel asked 2 weeks ago

I’ve a form in which I gather some data from user. I need to check if user has entered some text in the text field and not only white spaces.

1 Answers
Chaudhary Talha Staff answered 2 weeks ago

In swift 3 and later you can do this by adding String extension.

extension String {
     var isReallyEmpty: Bool {
         return self.trimmingCharacters(in: .whitespaces).count == 0
     }
 }

And later you can check this as:

if (nameOfUser.text?.isReallyEmpty)! {
             print("YES ITS WORKING")
         }