Danny Tan Danny Tan - 2 years ago 99
Swift Question

Boolean in an if statement for Swift

I am trying to write a boolean in an if statement for swift. It gives an error even though I explicitly check if it is false.

if (bool == false){
//asks to check for != nil

Here is the original code

var schoolName = ""
var NYUClicked : Bool = false
var CooperClicked : Bool = false
var TheNewSchoolClicked : Bool = false
var BaruchClicked : Bool = false
var FITClicked : Bool = false
var SVAClicked : Bool = false

// NYU
@IBOutlet weak var NYUButton: UIButton!

@IBAction func NYUClicked(sender: AnyObject) {
if ((CooperClicked || TheNewSchoolClicked || BaruchButton || FITClicked || SVAClicked) == false)

Answer Source

BaruchButton is not a Bool. You want BaruchClicked.

It's not common to check directly against true or false. Comparison with false is better done with a simple negation:

if !(CooperClicked || TheNewSchoolClicked || BaruchClicked || FITClicked || SVAClicked) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download