0

I am trying to check if a textbox has no value.

When I do this:

if(userEmail?.isEmpty || userPassword?.isEmpty || userPasswordRepeat?.isEmpty)  

I get the following error enter image description here

I tried adding "?" before the ".isEmpty" but the error won't go away enter image description here

Any ideas?

5
  • 1
    It's in the error message: Optional value not unwrapped. Use ! to unwrap a value and read up on optional values Commented Oct 10, 2015 at 8:40
  • @EICaptain Is there a difference between textfield.text and textfield.stringValue? Commented Oct 10, 2015 at 8:43
  • @EICaptain I'm asking. I'm used to using stringValue Commented Oct 10, 2015 at 8:46
  • 1
    @Arc676 stringValue property is not a member of textfiled when your develop app for iPhone or iPad...is available in mac app Commented Oct 10, 2015 at 8:49
  • This is a screenshot with the variables too. ge.tt/3NeaYbP2/v/0?c Commented Oct 10, 2015 at 8:53

2 Answers 2

1

Try this....

if txtEmail.text?.isEmpty == true || txtPassword.text?.isEmpty == true || txtRePassword.text?.isEmpty == true{
        print("true")
}
Sign up to request clarification or add additional context in comments.

2 Comments

It still doesn't work. This is the error message I recieve. ge.tt/8F7FbbP2/v/0?c
glad it helps...happy to help you :)
0

If interested also in positive case, the following is an alternative solution for Swift 2:

let email = self.txtEmail.text where !email.isEmpty, let password = self.txtPassword.text where !password.isEmpty {
    //all fields are not nil && not empty
}else{
    //some field is nil or empty
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.