ios - swift 3.0 如何在 Swift 3 中访问 `AnyHashable` 中的 `Any` 类型?

标签 ios swift3 fmdb hashable

我正在使用 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()
}

输出 enter image description here

最佳答案

这就是您访问 [AnyHashable : Any] 字典的方式

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

在你的情况下

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

关于ios - swift 3.0 如何在 Swift 3 中访问 `AnyHashable` 中的 `Any` 类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46294715/

相关文章:

ios - 使用OAuthConsumer在iOS上访问Yelp API

ios - Xcode 11 SPM 身份验证失败,因为未提供凭据

amazon-web-services - AWS 上的 Realm 对象服务器 (ROS) : how to make a valid Websocket request?

generics - Swift 3,元类型问题中的 ".self"是否真的正确?

ios - PdfKit 高亮注释

iphone - 重叠的 CAShapeLayer 轮廓

swift - 没有 "as CVarArg"无法将日期传递给 NSPredicate(格式 : . ..)

ios - 使用 NSArray 值高效创建占位符模板 NSString

xcode - FMDatabase/SQLite3 使用的 "Too many files open"问题

ios - FMResultSet 中的内存泄漏 - FMDB