swift 3.0 如何在 Swift 3 中访问 `Any` 中的 `AnyHashable` 类型?
swift 3.0 How can I access `AnyHashable` types in `Any` in Swift 3?
我正在使用 sqlite 文件从 authorId 获取 diaryEntriesTeacher。当我打印变量 authorId is nil 时,它会生成以下 authorId 对象
代码:-
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()
}
输出
这就是您访问 [AnyHashable : Any]
词典的方式
var dict : Dictionary = Dictionary<AnyHashable,Any>()
dict["name"] = "sandeep"
let myName : String = dict["name"] as? String ?? ""
你的情况
let authorId = totalCount?["authorId"] as? String ?? ""
我们需要在使用前将我们尝试访问的 属性 转换为 AnyHashable。
你的情况:
do {
let results = try objFMDB.executeQuery("select * from diaryEntriesTeacher", values: nil)
while results.next() {
let totalCount = results.resultDictionary
let authorId = totalCount?[AnyHashable("authorId")]!
print("authorId",authorId)
}
这是Swift。使用强类型和快速枚举。 Dictionary<AnyHashable,Any>
是字典的通用类型,可以转换为 <String,Any>
因为所有键似乎都是 String
.
do
if let results = try objFMDB.executeQuery("select * from diaryEntriesTeacher", values: nil) as? [[String:Any]]
for item in results {
let authorId = item["authorId"] as? String
let studentName = item["studentName"] as? String
print("authorId", authorId ?? "n/a")
print("studentName", studentName ?? "n/a")
}
}
....
我正在使用 sqlite 文件从 authorId 获取 diaryEntriesTeacher。当我打印变量 authorId is nil 时,它会生成以下 authorId 对象 代码:-
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()
}
输出
这就是您访问 [AnyHashable : Any]
var dict : Dictionary = Dictionary<AnyHashable,Any>()
dict["name"] = "sandeep"
let myName : String = dict["name"] as? String ?? ""
你的情况
let authorId = totalCount?["authorId"] as? String ?? ""
我们需要在使用前将我们尝试访问的 属性 转换为 AnyHashable。
你的情况:
do {
let results = try objFMDB.executeQuery("select * from diaryEntriesTeacher", values: nil)
while results.next() {
let totalCount = results.resultDictionary
let authorId = totalCount?[AnyHashable("authorId")]!
print("authorId",authorId)
}
这是Swift。使用强类型和快速枚举。 Dictionary<AnyHashable,Any>
是字典的通用类型,可以转换为 <String,Any>
因为所有键似乎都是 String
.
do
if let results = try objFMDB.executeQuery("select * from diaryEntriesTeacher", values: nil) as? [[String:Any]]
for item in results {
let authorId = item["authorId"] as? String
let studentName = item["studentName"] as? String
print("authorId", authorId ?? "n/a")
print("studentName", studentName ?? "n/a")
}
}
....