gpt4 book ai didi

swift - 使用非托管!作为 NSMutableDictionary 中的键

转载 作者:搜寻专家 更新时间:2023-10-31 08:19:17 29 4
gpt4 key购买 nike

我正在尝试创建一个钥匙串(keychain)查询,但我在使用属性项键作为字典键时遇到了困难。创建字典时,我可以像这样将包装在数组中的属性项作为字典键传递,没有任何问题

genericPasswordQuery = NSMutableDictionary(objects: [kSecClassGenericPassword, identifier], forKeys: [kSecClass, kSecAttrGeneric])

但是,如果我尝试像这样向查询字典添加另一个类似的项目:

genericPasswordQuery.setObject(accessGroup, key:kSecAttrAccessGroup)

它提示 key 不符合 NSCopying 并提供类型错误:

“找不到接受所提供参数的‘setObject’的重载”

这是 SecItemAdd 的标准实现,但我在 Swift 中遇到问题。

最佳答案

我想我找到了解决办法。来自docs :

When Swift imports APIs that have not been annotated, the compiler cannot automatically memory manage the returned Core Foundation objects. Swift wraps these returned Core Foundation objects in an Unmanaged structure. All indirectly returned Core Foundation objects are unmanaged as well.

When you receive an unmanaged object from an unannotated API, you should immediately convert it to a memory managed object before you work with it. That way, Swift can handle memory management for you. The Unmanaged structure provides two methods to convert an unmanaged object to a memory managed object—takeUnretainedValue() and takeRetainedValue().

当前实现:

genericPasswordQuery = NSMutableDictionary(objects: [kSecClassGenericPassword, identifier], forKeys: [kSecClass, kSecAttrGeneric])

var kSecAttrAccessGroupSwift: NSString = kSecAttrAccessGroup.takeRetainedValue() as NSString
genericPasswordQuery.setObject(accessGroup, forKey: kSecAttrAccessGroupSwift)

这在 Xcode 中运行良好,但当我添加 .takeRetainedValue 时,Playground 立即崩溃

关于swift - 使用非托管<AnyObject>!作为 NSMutableDictionary 中的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24215757/

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