UnsafeMutablePointer< CFTypeRef>在Swift 3中 [英] UnsafeMutablePointer<CFTypeRef> in Swift 3

查看:122
本文介绍了UnsafeMutablePointer< CFTypeRef>在Swift 3中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在钥匙串实用程序类中调用SecItemCopyMatching以便从钥匙串中获取数据,但是在获取result参数UnsafeMutablePointer<CFTypeRef?>时遇到了问题.

I'm attempting to call SecItemCopyMatching in my keychain utility class in order to get data out of the keychain, yet I'm running into a problem with getting the result argument, UnsafeMutablePointer<CFTypeRef?>.

原始语句(在Swift 2中,迁移到Swift 3之前)是

The original statement (in Swift 2, before migrating to Swift 3) was

// 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告诉您的操作,我尝试了

But in Swift 3, you are now required to call .withMemoryRebound in order to view memory. Based on what Xcode tells you to do, I tried this

var result: Data?
let status = withUnsafeMutablePointer(to: &result){
    $0.withMemoryRebound(to: Data.self, capacity: 1){
        SecItemCopyMatching(query as CFDictionary, UnsafePointer($0))
    }
}

但是这样做,我得到一个错误

Yet doing this, I get an error

无法将类型"UnsafePointer<_>"的值转换为预期的参数类型"UnsafeMutablePointer<CFTypeRef?>?"

Cannot convert value of type 'UnsafePointer<_>' to expected argument type 'UnsafeMutablePointer<CFTypeRef?>?'

因此,我尝试使用CFTypeRef而不是Data

So, I tried using CFTypeRef instead of Data

var result: CFTypeRef?
let status = withUnsafeMutablePointer(to: &result){
    $0.withMemoryRebound(to: CFTypeRef.self, capacity: 1){
        SecItemCopyMatching(query as CFDictionary, UnsafePointer($0))
    }
}

简单地用$0替换UnsafePointer($0)会导致相同的错误消息.

Replacing UnsafePointer($0) with simply $0 results in the same error message.

如何获取UnsafeMutablePointer<CFTypeRef?>从钥匙串获取数据?

How can I get an UnsafeMutablePointer<CFTypeRef?> for getting data from keychain?

推荐答案

在您的情况下,您无需使用withMemoryReboundwithUnsafeMutablePointer(to:).

In your case, you do not need to use withMemoryRebound or withUnsafeMutablePointer(to:).

相反,您只能使用

var result: AnyObject?
let status = SecItemCopyMatching(query as CFDictionary, &result)

if status == noErr, let data = result as? Data {
    //use data...
}

通常,当您需要将UnsafeMutablePointer<T>传递给函数时,请声明类型为T的变量,并将其作为inout参数&variable传递.在您的情况下,TCFTypeRef?,而在Swift 3中,CFTypeRef只是AnyObject的类型别名.

Generally, when you need to pass an UnsafeMutablePointer<T> to a function, declare a variable of type T and pass it as an inout argument &variable. In your case, T is CFTypeRef?, and in Swift 3, CFTypeRef is just a typealias of AnyObject.

即使在Swift 2.2中,您也不需要使用withUnsafeMutablePointer

Even in Swift 2.2, you did not need to use withUnsafeMutablePointer

var result: AnyObject?  
let status = SecItemCopyMatching(query, &result)  

这篇关于UnsafeMutablePointer&lt; CFTypeRef&gt;在Swift 3中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆