0

阅读 Apple 的代码,我看到了以下内容(谈论钥匙串服务):

query[kSecAttrService as String] = service as AnyObject?

的目的是AnyObject?什么?

我认为它可以简化为

query[kSecAttrService as String] = service as AnyObject

有什么线索吗?

这是来自 Apple 示例的整个片段:

private static func keychainQuery(withService service: String, account: String? = nil, accessGroup: String? = nil) -> [String : AnyObject] {
    var query = [String : AnyObject]()
    query[kSecClass as String] = kSecClassGenericPassword
    query[kSecAttrService as String] = service as AnyObject?

    if let account = account {
        query[kSecAttrAccount as String] = account as AnyObject?
    }

    if let accessGroup = accessGroup {
        query[kSecAttrAccessGroup as String] = accessGroup as AnyObject?
    }

    return query
}
4

1 回答 1

2

也许片段来自一些 Swift 2 代码,在 Swift 3+ 它是

private static func keychainQuery(withService service: String, account: String? = nil, accessGroup: String? = nil) -> [String : Any] {
    var query = [String : Any]()
    query[kSecClass as String] = kSecClassGenericPassword
    query[kSecAttrService as String] = service

    if let account = account {
        query[kSecAttrAccount as String] = account
    }

    if let accessGroup = accessGroup {
        query[kSecAttrAccessGroup as String] = accessGroup
    }

    return query
}
于 2018-10-27T16:57:10.767 回答