不能传递'UnsafeRawPointer?'类型的不可变值作为inout arguement

时间:2017-03-29 05:33:17

标签: swift

var keyCallbacks = kCFTypeDictionaryKeyCallBacks         var valueCallbacks = kCFTypeDictionaryValueCallBacks

    var keys = [Unmanaged.passUnretained(kSecClass).toOpaque(), Unmanaged.passUnretained(kSecAttrApplicationTag).toOpaque(), Unmanaged.passUnretained(kSecAttrKeyType).toOpaque(), Unmanaged.passUnretained(kSecReturnRef).toOpaque()]
    var values = [Unmanaged.passUnretained(kSecClassKey).toOpaque(), Unmanaged<AnyObject>.passUnretained(privateTag as AnyObject).toOpaque(), Unmanaged.passUnretained(kSecAttrKeyTypeRSA).toOpaque(), Unmanaged.passUnretained(kCFBooleanTrue).toOpaque()]
    //let queryKey = CFDictionaryCreate(kCFAllocatorDefault, &keys, &values, 4, &keyCallbacks, &valueCallbacks)//Error-- for "keys" this error show cannot pass immutable value of type 'UnsafeRawPointer?' as inout arguement. 
    let queryKey = CFDictionaryCreate(kCFAllocatorDefault, &keys, &values, 4, &keyCallbacks, &valueCallbacks)

    // Get the key.
    var extractedItem: AnyObject?
    let resultCode = SecItemCopyMatching(queryKey, &extractedItem)//cannot pass immutable value of type 'UnsafeRawPointer?' as inout arguement

    guard let keyItem = extractedItem, resultCode == noErr else {

        return nil
    }

    let result = keyItem as! SecKey
    return result

1 个答案:

答案 0 :(得分:0)

不要在Swift中使用CFDictionary。这不是不可能,但不是 值得麻烦。 CFDictionary免费桥接到NSDictionary 而这又可以从Swift Dictionary实例化:

let query: NSDictionary = [
    kSecClass: kSecClassKey,
    kSecAttrApplicationTag: privateTag,
    kSecAttrKeyType: kSecAttrKeyTypeRSA,
    kSecReturnRef: true
]
var extractedItem: AnyObject?
let resultCode = SecItemCopyMatching(query, &extractedItem)