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")
}
luk2302
  • 55,258
  • 23
  • 97
  • 137
ytpm
  • 4,962
  • 6
  • 56
  • 113

5 Answers5

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")
}
Devster101
  • 578
  • 4
  • 18
  • that shows an error: `Bound value in a conditional binding must be of Optional type` on `== ` – ytpm Sep 09 '15 at 15:08
  • 2
    Just because you are the first one that answered and helped me, I've accepted your answer. Thanks! – ytpm Sep 09 '15 at 15:32
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.

Community
  • 1
  • 1
whyceewhite
  • 6,317
  • 7
  • 43
  • 51
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.

Michal
  • 15,429
  • 10
  • 73
  • 104
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")
}
vadian
  • 274,689
  • 30
  • 353
  • 361
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") 
    }
} 
Dev
  • 1,215
  • 9
  • 17