Question: I can't seem to find a solution to this on the other questions. 

I am using the Swift Locksmith (<https://github.com/matthewpalmer/Locksmith>) library.

Here is what I have:

```
let dictionary = Locksmith.loadDataForUserAccount("AppData")

```

When I print out dictionary I get:

```
Optional(["username": test, "password": test123])

```

However, When I try to assign those values to variables to pass long I seem to his a mishap and get the error:

> 
> fatal error: unexpectedly found nil while unwrapping an Optional value
> 
> 
> 

I tried to assign it like so:

```
username = dictionary["username"]

```

Which told me:

> 
> Type '[String : AnyObject]?' has no subscript members
> 
> 
> 

I tried to use .stringValue like so: 

```
dictionary["username"].stringValue

```

Xcode then told me to 'Fix it", so I clicked the fix it button and then xcode gave me this: 

```
username = dictionary!["username"]!.stringValue

```

tl;dr So, my question
=====================

How do I get the username and password out of the dictionary (Keychain Item) and assign them to a variable, so that I can pass them to a new view?

Answer: 
There are two optionals involved: the dictionary itself (returned by `loadDataForUserAccount`), and the result of each subscript (like `["username"]` or `["password"]`). When you are dealing with many optionals, I recommend avoiding the `!` operator entirely. You should only use that when you're *sure* the result will *never* be `nil`. And since you're dealing with the keychain, this is not guaranteed. 

Instead, you should use `if let` or `guard let` to unwrap each object you need, and proceed only if you get the result you are looking for. Here's an example using `guard let`, which is what I think you probably want:

```
func authenticate() {
    guard let dictionary = Locksmith.loadDataForUserAccount("AppData"),
          let username = dictionary["username"],
          let password = dictionary["password"] else {
            // nothing stored in keychain, the user is not authenticated
            return
    }

    print("username is \(username).")
    print("passsword is \(password).")
}

```

You can also rewrite this using `if let`:

```
func authenticate() {
    if let dictionary = Locksmith.loadDataForUserAccount("AppData"),
       let username = dictionary["username"],
       let password = dictionary["password"] {
        print("username is \(username).")
        print("password is \(password).")
    } else {
        // nothing stored in keychain, the user is not authenticated
    }
}

```

I prefer `guard let` in this instance because it more clearly expresses to a reader of your code what the optimal/ideal code path is.
Your dictionary is optional.
to get the user name you can do something like 

```
let username = dictionary!["username"]

```

or

```
let userNameString =  dictionary!["username"] as! String

```