我正在尝试调用SecItemCopyMatching
我的钥匙串实用程序类以从钥匙串中获取数据,但我在获取result
参数时遇到了问题,UnsafeMutablePointer<CFTypeRef?>
.
最初的声明(在 Swift 2 中,在迁移到 Swift 3 之前)是
// query is a dictionary of [String : AnyObject]
var result: Data?
let status = withUnsafeMutablePointer(to: &result) {
SecItemCopyMatching(query as CFDictionary, UnsafeMutablePointer($0))
}
但在 Swift 3 中,您现在需要调用.withMemoryRebound
才能查看内存。根据 Xcode 告诉您的操作,我尝试了这个
var result: Data?
let status = withUnsafeMutablePointer(to: &result){
$0.withMemoryRebound(to: Data.self, capacity: 1){
SecItemCopyMatching(query as CFDictionary, UnsafePointer($0))
}
}
然而这样做,我得到一个错误
无法将类型“
UnsafePointer<_>
”的值转换为预期的参数类型“UnsafeMutablePointer<CFTypeRef?>?
”
所以,我尝试使用CFTypeRef
而不是Data
var result: CFTypeRef?
let status = withUnsafeMutablePointer(to: &result){
$0.withMemoryRebound(to: CFTypeRef.self, capacity: 1){
SecItemCopyMatching(query as CFDictionary, UnsafePointer($0))
}
}
替换UnsafePointer($0)
为简单$0
会导致相同的错误消息。
如何UnsafeMutablePointer<CFTypeRef?>
从钥匙串中获取数据?