1

After signing in successfully I want to get the value of a particular attribute. Is it possible to do this without iterating through each attribute? I know I can't subscript but is there any other way?

So in the example below what I want is the value of the user's email. Ideally, I would like to say let email = userAttributes["email"] but this type does not allow subscription.

user?.getDetails().continueOnSuccessWith(block: { (task) -> Any? in
    let userAttributes = task.result?.userAttributes

    for attribute in userAttributes! {
        print(attribute.name!)
        print(attribute.value!)
    }
    return nil
})
Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
alionthego
  • 8,508
  • 9
  • 52
  • 125

1 Answers1

0

Seeing that AWSCognitoIdentityProviderAttributeType contains both a name and a value, we can easily map over that data to make a dictionary that can be accessed by a key value.

Add this extension somewhere

extension Array where Element: AWSCognitoIdentityProviderAttributeType {
    func dict() -> [String : String] {
        var dict: [String : String] = [:]
        self.forEach({ e in dict[e.name!] = e.value! })
        return dict
    }
}

then use

task.result?.userAttributes.dict()[...]