Pointer casting with ARC

前端 未结 3 1571
一生所求
一生所求 2021-02-03 22:32

ARC is giving me a hard time with following cast:

NSDictionary *attributes;
SecItemCopyMatching((__bridge CFDictionaryRef)keychainItemQuery, (CFTypeRef *)&at         


        
相关标签:
3条回答
  • 2021-02-03 22:38

    Need to change attributes to &attributes

    CFDataRef attributes;
    SecItemCopyMatching((__bridge CFDictionaryRef) keychainItemQuery,  ( CFTypeRef*) &attributes);
    NSData* passDat=(__bridge_transfer NSData*) attributes;
    
    0 讨论(0)
  • 2021-02-03 22:40

    The problem is that attributes shouldn't be a dictionary, it should be a SecKeyRef or CFDataRef. And then cast that back into NSData for the password data copied into it.

    Like so:

    CFDataRef attributes;
    SecItemCopyMatching((__bridge CFDictionaryRef)keychainItemQuery, (CFTypeRef *)&attributes);
    NSData *passDat = (__bridge_transfer NSData *)attributes;
    
    0 讨论(0)
  • 2021-02-03 22:47

    As we were doing something similar things and using the example above, we were facing another problem:

    CFDataRef resultRef;
    OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)searchDictionary,
                   (CFTypeRef *)&resultRef);
    NSData* result = (__bridge_transfer NSData*)resultRef; 
    

    This will result in an EXEC_BAD_ACCESS, because resultRef is not set to any adress and points somewhere to the memory.

    CFDataRef resultRef = nil;
    

    This will fix the error.

    0 讨论(0)
提交回复
热议问题