Error casting from AnyObject? to SecKeyRef? in Swift

eharo2

As part of a Swift based PKI implementation, I ported the SecKeyWrapper.m to Swift

Both, (Objective-C and Swift) of the Keychain access methods are working fine except one part in the Swift implementation.

I receive the public key from the peer in base64 format. It is converted to NSData and then saved in the Keychain using:

statusCode = SecItemAdd(queryDictionary, &persistentPeer)

After successfully saving the peer public key in the keychain, I need to get it as SecKeyRef.

    queryDictionary[kSecReturnPersistentRef] = true
    var peerKeyRef: AnyObject?
    statusCode = SecItemCopyMatching(queryDictionary, &peerKeyRef)

The problem is that SecItemCopyMatching returns an object of type AnyObject? than can´t be casted to SecKeyRef (or SecKey thats is an alias)

let key = peerKeyRef as? SecKeyRef // Compile error.

The work around I found is to cast the peerKeyRef as NSObject? and then use Objective-C to bridge to SecKey as follows:

let keyObject = peerKeyRef as? NSObject
keyRef = Utility.getSecKeyRefFromAnyObject(keyObject).takeRetainedValue()

The Objetive-C code to do it is as follows:

+(SecKeyRef) getSecKeyRefFromNSObject:(id)theObject {
    return (__bridge SecKeyRef)theObject;
}

The casting to SecKey using this workaround works fine (no warnings or errors) with the expected result.

What do you think is missing in the Swift code to cast AnyObject? to SecKeyRef?

I am working with Xcode 7.3.1 and Swift 2.2

Rgds....

OOPer

That compile error seems to be a bug to me. (Which was once a valid diagnosis when all CF types were imported as typealiases of AnyObject.)

But in your case I would just do it as:

let key = peerKeyRef as! SecKeyRef?

You are 100% sure that the query returns SecKeyRef or nil, no?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Casting AnyObject to Dictionary in swift

From Dev

Downcast from AnyObject to UIImage[] - Swift

From Dev

Obtain public key from private SecKeyRef

From Dev

xcode6 swift NSObject AnyObject error

From Dev

Swift can print AnyObject, but can't create String from AnyObject

From Dev

Casting a viewController extracted from an UINavigationController in Swift

From Dev

Swift - AnyObject[] is not a subtype of AnyObject[]?

From Dev

String is not convertible from Dictionary<String,AnyObject> Error in Swift

From Dev

Swift typecast from AnyObject

From Dev

Swift issue: About Type Casting for AnyObject

From Dev

Compiler error when casting Dictionary to AnyObject?

From Dev

Casting Any to AnyObject in Swift

From Dev

Casting AnyObject to Double

From Dev

AFNetworking swift JSON AnyObject casting

From Dev

Get Value from AnyObject Response Swift

From Dev

Casting from AnyObject to CGColor? without errors or warnings

From Dev

iOS error casting Object in Swift which inherited from an Obj-C Class

From Dev

Siesta Swift Casting from ImplicitlyUnwrappedOptional<Swift.AnyObject> to Array<AnyObject>

From Dev

Swift AnyObject - Down casting an array of protocols to [AnyObject]

From Dev

How to extract RSA parameters from iOS SecKeyRef?

From Dev

Casting AnyObject to T

From Dev

Swift class cast fails while casting AnyObject to NSMutableDictionary

From Dev

Optional casting of an AnyObject into Array gives an error

From Dev

Swift creating JSON data from AnyObject

From Dev

Casting String to [NSObject : AnyObject]

From Dev

Casting an AnyObject to a Dictionary nested in an Array

From Dev

Swift 3 type casting error

From Dev

How to get a SecIdentityRef from a SecCertificateRef and a SecKeyRef

From Dev

Error casting from Int to Double

Related Related

  1. 1

    Casting AnyObject to Dictionary in swift

  2. 2

    Downcast from AnyObject to UIImage[] - Swift

  3. 3

    Obtain public key from private SecKeyRef

  4. 4

    xcode6 swift NSObject AnyObject error

  5. 5

    Swift can print AnyObject, but can't create String from AnyObject

  6. 6

    Casting a viewController extracted from an UINavigationController in Swift

  7. 7

    Swift - AnyObject[] is not a subtype of AnyObject[]?

  8. 8

    String is not convertible from Dictionary<String,AnyObject> Error in Swift

  9. 9

    Swift typecast from AnyObject

  10. 10

    Swift issue: About Type Casting for AnyObject

  11. 11

    Compiler error when casting Dictionary to AnyObject?

  12. 12

    Casting Any to AnyObject in Swift

  13. 13

    Casting AnyObject to Double

  14. 14

    AFNetworking swift JSON AnyObject casting

  15. 15

    Get Value from AnyObject Response Swift

  16. 16

    Casting from AnyObject to CGColor? without errors or warnings

  17. 17

    iOS error casting Object in Swift which inherited from an Obj-C Class

  18. 18

    Siesta Swift Casting from ImplicitlyUnwrappedOptional<Swift.AnyObject> to Array<AnyObject>

  19. 19

    Swift AnyObject - Down casting an array of protocols to [AnyObject]

  20. 20

    How to extract RSA parameters from iOS SecKeyRef?

  21. 21

    Casting AnyObject to T

  22. 22

    Swift class cast fails while casting AnyObject to NSMutableDictionary

  23. 23

    Optional casting of an AnyObject into Array gives an error

  24. 24

    Swift creating JSON data from AnyObject

  25. 25

    Casting String to [NSObject : AnyObject]

  26. 26

    Casting an AnyObject to a Dictionary nested in an Array

  27. 27

    Swift 3 type casting error

  28. 28

    How to get a SecIdentityRef from a SecCertificateRef and a SecKeyRef

  29. 29

    Error casting from Int to Double

HotTag

Archive