swift 3.0 如何在 Swift 3 的 `Any` 中访问 `AnyHashable` 类型?
问题描述:
我正在使用 sqlite 文件从 authorId 获取 diaryEntriesTeacher.当我打印变量 authorId 为 nil 时,它生成以下 authorId 对象代码:-
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()
}
输出
答
This is how you access Dictionary of [AnyHashable : Any]
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 ?? ""
就你而言
let authorId = totalCount?["authorId"] as? String ?? ""