1

I have some code I am using for keychain management. The SecItemCopyMatching method returns the result in (CFTypeRef *) type called foundDict, which is passed as a reference to the SecItemCopyMatching method. Then I am using (__bridge_transfer NSData*) to bridge from the CFTypeRef* to an NSData* (into "result" variable). So I was expecting that the result variable will of course be of type NSData, because I declared it so, and used the bridge to convert CFTypeRef* to NSData*. But when I want to convert the NSData into a string using the NSString method: initWithBytes:length:encoding, I get a runtime error that tells me "[__NSCFArray bytes]: unrecognized selector set to instance.."

When I looked in the debugger in Xcode , I can see why it is complaining because the "result" variable was converted from NSData to __NSCFArray after the "bridge" statement. And since __NSCFArray is an array type it does not support a method called "bytes" and thus the runtime complains

So, I don't understand why internally this conversion is happening from NSData to __NSCFArray And most importantly, how is the proper way to convert the results returned by the "SecItemCopyMatching" which is of type CFTypeRef to NSData. The funny thing is that i am following the exact same code suggested by Apple in its keychain wrapper example at KeychainWrapper Sample code

Any thoughts?

I am attaching an image of the debugger:

enter image description here

malena
  • 798
  • 11
  • 26
  • 1
    `CFTypeRef` is like `id`, it can be anything, in your example `result` is an `NSArray` object containing a dictionary with 10 key/value pairs. Bridge to `NSArray` and log `result` to check. – vadian Jan 10 '16 at 19:12
  • Thanks, yes, I should have used NSArray because i was asking for the return of all attributes (array of key/value pairs) from the keychain , not just one value. I confirmed by using NSArray instead of NSData and all is fine now . – malena Jan 10 '16 at 20:44
  • https://stackoverflow.com/questions/17822895/how-to-cast-an-indirect-pointer-in-objective-c – evandrix Mar 18 '19 at 19:41

0 Answers0