gpt4 book ai didi

swift - 元素作为 AnyObject vs 元素 vs AnyObject?

转载 作者:行者123 更新时间:2023-11-28 11:43:31 25 4
gpt4 key购买 nike

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

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
}

最佳答案

也许该片段来自一些 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
}

关于swift - 元素作为 AnyObject vs 元素 vs AnyObject?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53024154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com