UnsafeMutablePointer<CFTypeRef> in Swift 3

Jojodmo

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?>.

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))
}

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

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

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))
    }
}

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

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

OOPer

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

Instead, you can just use

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

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

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.


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

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

UnsafeMutablePointer to expected argument type UnsafeMutablePointer<_> in Swift 3

How to return a dictionary through a `UnsafeMutablePointer<CFTypeRef?>?` parameter?

Error in UnsafeMutablePointer in swift3

How to use UnsafeMutablePointer in Swift 3?

UnsafeMutablePointer<Bytef> swift3

Convert UnsafeMutableRawPointer to UnsafeMutablePointer<T> in swift 3

Casting a variable of type 'UnsafeMutableRawPointer' to UnsafeMutablePointer<> in Swift 3

How to use UnsafeMutablePointer<T?> in swift 3

How to create UnsafeMutablePointer<UnsafeMutablePointer> in swift

Swift UnsafeMutablePointer & UnsafeMutablePointer<UnsafePointer<SomeType>>

how to cast from CFTypeRef to AXUIElement in Swift

How to do pointer arithmetic on UnsafeMutablePointer and get String in Swift 3

How to convert String to UnsafeMutablePointer<UInt8> ? Swift 3

Swift 2 - UnsafeMutablePointer<Void> to object

Using UnsafeMutablePointer and CFRunLoopObserverContext in swift 2

Cast a Swift struct to UnsafeMutablePointer<Void>

What is UnsafeMutablePointer<Unmanaged<CMSampleBuffer>?> in Swift?

How to use UnsafeMutablePointer<OpaquePointer> in Swift?

How to convert a type of "any" in kind "UnsafeMutablePointer<Int16>" in a swift 3

Swift: Cannot convert value of type 'UnsafeMutablePointer' to expected argument type 'UnsafeMutablePointer'

In Swift 3.1, UnsafeMutablePointer.initialize(from:) is deprecated

Swift convert Data to UnsafeMutablePointer<Int8>

Cast a swift dictionary ( [String:AnyObject] ) to a UnsafeMutablePointer

How to create UnsafeMutablePointer<CGPoint> Object in swift

How to cast self to UnsafeMutablePointer<Void> type in swift

Swift convert string to UnsafeMutablePointer<Int8>

Cannot assign value of type UnsafeMutablePointer ObjCBool in Swift

How to dealloc UnsafeMutablePointer referenced from Swift struct

Memset to UnsafeMutablePointer<UInt8> in swift