Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift 2.0 keychain type errors for SecItemCopyMatching

We had this snippet of code with the previous version of Swift

    var retrievedData: NSData?
    var extractedData: Unmanaged<AnyObject>? = nil
    let status = SecItemCopyMatching(keyChainQuery, &extractedData)

    if (status == errSecSuccess) {
        if let validExtractedData = extractedData {
            let opaque = validExtractedData.toOpaque()
            retrievedData = Unmanaged<NSData>.fromOpaque(opaque).takeUnretainedValue()
        }
    }

However this now gives us the following error:

Cannot convert value of type 'inout Unmanaged?' (aka 'inout Optional>') to expected argument type 'UnsafeMutablePointer' (aka 'UnsafeMutablePointer>')

I'm a bit lost on how to fix it. I presume I have to allocate some memory, make an UnsafeMutablePointer to it and then supply this to SecItemCopyMatching?

I tried this:

    let sizeOfKey = 32
    var store = NSMutableData(capacity: sizeOfKey)!
    let status = SecItemCopyMatching(keyChainQuery, store.bytes)

But this gave me an:

Cannot convert value of type 'UnsafePointer' (aka 'UnsafePointer<()>') to expected argument type 'UnsafeMutablePointer' (aka 'UnsafeMutablePointer>') error

Anyone an idea?

like image 999
Toad Avatar asked Sep 03 '15 14:09

Toad


1 Answers

It seems, we don't need Unmanaged<> work anymore.

Try:

var retrievedData: NSData?
var extractedData: AnyObject?
let status = SecItemCopyMatching(keyChainQuery, &extractedData)

if (status == errSecSuccess) {
    retrievedData = extractedData as? NSData
}
like image 54
rintaro Avatar answered Oct 24 '22 03:10

rintaro