Muhammad Umar
Muhammad Umar

Reputation: 11782

Logical AND operations in Swift not working

I am new to swift and trying to solve a very basic Logical AND problem

if (textField == self.cvv && cvv.text.length == 4 && !string.isEmpty)
{
    return false;
}

this is my code

According to this https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/BasicOperators.html

the && does exists however I am getting error

Couldn't find an overload for the &&

What and how can I use logical operations?

Upvotes: 2

Views: 733

Answers (2)

SwiftArchitect
SwiftArchitect

Reputation: 48514

It is not a logical && error: compiler confused by an earlier error:

'String' does not have a member named 'length'


count(cvv.text) is not available either:

there is no universally good answer, see the documentation comment for discussion

See @RMenke's answer for Swift 2.0 syntax.

Upvotes: 5

R Menke
R Menke

Reputation: 8391

I just tried this:

var textField = UITextField()

var cvv = UITextField()

var string = ""


if (textField == cvv && cvv.text!.characters.count == 4 && string.isEmpty)
{
    return false;
}

I couldn't replicate the error, because I have no idea about the types and declarations of all instances involved. However I got an error on the text.length might be a Swift 1.2 => swift 2.0 change. I updated it to text.characters.count

To more fully answer your question... && always worked fine for me, exactly the way you use it. conditon1 operatorA condition2 && condition3 operatorB conditon4 ...

However the "couldn't find an overload" error is often the result of a type mismatch. Checking an Int against a String for example.

Upvotes: 2

Related Questions