swift 3.0 How can I access `AnyHashable` types in `Any` in Swift 3?

Jayprakash Singh picture Jayprakash Singh · Sep 19, 2017 · Viewed 9.8k times · Source

I'm using sqlite file to get the diaryEntriesTeacher from the authorId. it generates the following object of authorId when I print the variable authorId is nil Code :-

func applySelectQuery() {        
    checkDataBaseFile()
    objFMDB = FMDatabase(path: fullPathOfDB)
    objFMDB.open()
    objFMDB.beginTransaction()

    do {
        let results = try objFMDB.executeQuery("select * from diaryEntriesTeacher", values: nil)



        while results.next() {  
            let totalCount = results.resultDictionary
            let authorId = totalCount?["authorId"]! 
            print("authorId",authorId)
   }


    }
    catch {
        print(error.localizedDescription)
    }
    print(fullPathOfDB)
    self.objFMDB.commit()
    self.objFMDB.close()
}

output enter image description here

Answer

Sandeep Bhandari picture Sandeep Bhandari · Sep 19, 2017

This is how you access Dictionary of [AnyHashable : Any]

var dict : Dictionary = Dictionary<AnyHashable,Any>()
dict["name"] = "sandeep"
let myName : String = dict["name"] as? String ?? ""

In your case

let authorId = totalCount?["authorId"] as? String ?? ""