Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
732 views
in Technique[技术] by (71.8m points)

swift - Get Anyhashable type of data?

How to get userid using for loop from the Anyhashable type of data?

    //This is the data and I want to get all of the userid from this data

? some : 6 elements
    ? 0 : 2 elements
      ? key : AnyHashable("company_name")
        - value : "company_name"
      - value : Company 2549
    ? 1 : 2 elements
      ? key : AnyHashable("contact_unique_id")
        - value : "contact_unique_id"
      - value : 230679
    ? 2 : 2 elements
      ? key : AnyHashable("Client_Phone")
        - value : "Client_Phone"
      - value : 999999
    ? 3 : 2 elements
      ? key : AnyHashable("userid")
        - value : "userid"
      - value : 7295
    ? 4 : 2 elements
      ? key : AnyHashable("full_name")
        - value : "full_name"
      - value : A. B. M
    ? 5 : 2 elements
      ? key : AnyHashable("email_address")
        - value : "email_address"
      - value : wwsum.mar.com

 if Utils.getSharedManagerObject().arraySelectContacts.count > 0 {
                let contactDictionary = Utils.getSharedManagerObject().arraySelectContacts[0] as? [AnyHashable : Any]
                let contactName       = contactDictionary!["full_name"] as! String

            let toBeDisplayedText = Utils.getSharedManagerObject().arraySelectContacts.count > 1 ? "(contactName) + (Utils.getSharedManagerObject().arraySelectContacts.count - 1) more" : contactName                self.btnSelectContact.setTitle(toBeDisplayedText  , for: UIControlState.normal)
            self.btnSelectContact.setTitleColor(UIColor.black , for: UIControlState.normal)
            }
            else {
            self.btnSelectContact.setTitle("Tap to Select Contact(s)", for: UIControlState.normal)
            self.btnSelectContact.setTitleColor(UIColor.gray , for: UIControlState.normal)
            }
        if Utils.getSharedManagerObject().arraySelectAssignees.count > 0   {
            let contactDictionary = Utils.getSharedManagerObject().arraySelectAssignees[0] as? [AnyHashable : Any]

//I am try to get userid from this code

            let contactName       = contactDictionary!["full_name"] as! String
                           if let users = contactDictionary as? [String : Any]   {
                            print(users)
                            for user in users  {
                                print(user)

                                       }
                                       }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can simply use if-let to fetch the required value from contactDictionary, i.e.

if let userId = contactDictionary?["userid"] as? String {
    //use userId here...
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...