0

I must doing something wrong, because inside the NSDictionary the values are 1 or 0 and it always prints "locked"...

let object: NSDictionary = self.collectionObjects?.objectAtIndex(indexPath.row) as! NSDictionary
if let locked = (object.objectForKey("locked") as? NSNumber)?.boolValue {
    println("locked")
} else {
    println("open")
}

5 Answers 5

2

Your not testing anything there

Try,

let object: NSDictionary = self.collectionObjects?.objectAtIndex(indexPath.row) as! NSDictionary
let locked = (object.objectForKey("locked") as? NSNumber)?.boolValue
if locked == true
{  
    println("locked")
}
else
{
    println("open")
}
Sign up to request clarification or add additional context in comments.

2 Comments

that shows an error: Bound value in a conditional binding must be of Optional type on ==
Just because you are the first one that answered and helped me, I've accepted your answer. Thanks!
2

When you are doing:

if let locked = (object.objectForKey("locked") as? NSNumber)?.boolValue {
    ...
} else {
    ...
}

The if statement is checking whether or not your locked variable has evaluated to a nil value. If it has a value then the true statement block will be executed; if it has nil then the false block (else statement) will be executed.

This is called Optional Binding.

You use optional binding to find out whether an optional contains a value, and if so, to make that value available as a temporary constant or variable. Optional binding can be used with if and while statements to check for a value inside an optional, and to extract that value into a constant or variable, as part of a single action.

As a result, if your variable does not evaluate to nil then you could use the locked variable inside the if block.

For example:

if let locked = (object.objectForKey("locked") as? NSNumber)?.boolValue {
    if locked {
        println("locked")
    } else {
        println("open")
    }
} else {
    println("locked is nil")
}

See this SO post for an example.

Comments

1

What you're doing is unwrapping the conditional value, not checking it, so what you need to do is:

  1. Unwrap it
  2. Test it

Handily, the if-let-where statement can do it for us in one line like this:

if let object = self.collectionObjects?.objectAtIndex(indexPath.row) as? NSDictionary, locked = object.objectForKey("locked")?.boolValue where locked {
    // we have a value here and it happens to be true
} else {
    // this means we don't have a value
}

Include the first let object in the if statement too - you'll never know, it can be null and then it would unexpectedly crash.

Also, the compiler should be able to handle object -> boolValue directly, if not, add the NSNumber casting back.

Comments

1

I recommend this

let object: NSDictionary = self.collectionObjects?.objectAtIndex(indexPath.row) as! NSDictionary
if let locked = object.objectForKey("locked") as? NSNumber where locked.boolValue == true {
  print("locked")
}else {
  print("open")
}

Comments

1

You are checking only the value is whether nil or not nil. As the value is not nil you are always getting output as "locked".

Try this

let object: NSDictionary = self.collectionObjects?.objectAtIndex(indexPath.row) as! NSDictionary 
if let locked = (object.objectForKey("locked") as? NSNumber)?.boolValue { 
    If locked == true {
        println("locked")
    }
    else { 
        println("open") 
    }
} 

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.