swift – 使用Unmanaged!作为NSMutableDictionary的关键

swift – 使用Unmanaged!作为NSMutableDictionary的关键,第1张

概述我正在尝试创建一个keychain查询,但我在使用Attribute Item Keys作为字典键时遇到了困难.在创建字典时,我可以将包含在数组中的属性项作为字典键传递,如此没有任何问题 genericPasswordQuery = NSMutableDictionary(objects: [kSecClassGenericPassword, identifier], forKeys: [kSec 我正在尝试创建一个keychain查询,但我在使用Attribute Item Keys作为字典键时遇到了困难.在创建字典时,我可以将包含在数组中的属性项作为字典键传递,如此没有任何问题

genericPasswordquery = NSMutableDictionary(objects: [kSecclassGenericPassword,IDentifIEr],forKeys: [kSecclass,kSecAttrGeneric])

但是,如果我尝试将另一个类似项添加到查询字典中,如下所示:

genericPasswordquery.setobject(accessGroup,key:kSecAttrAccessGroup)

它抱怨密钥不符合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,kSecAttrGeneric])var kSecAttrAccessGroupSwift: Nsstring = kSecAttrAccessGroup.takeRetainedValue() as NsstringgenericPasswordquery.setobject(accessGroup,forKey: kSecAttrAccessGroupSwift)

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

总结

以上是内存溢出为你收集整理的swift – 使用Unmanaged!作为NSMutableDictionary的关键全部内容,希望文章能够帮你解决swift – 使用Unmanaged!作为NSMutableDictionary的关键所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1008450.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存