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的关键所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)