Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
922 views
in Technique[技术] by (71.8m points)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...