简体   繁体   English

用ARC指针铸造

[英]Pointer casting with ARC

ARC is giving me a hard time with following cast: ARC让我很难跟随演员:

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

Error: Cast of an indirect pointer to an Objective-C pointer to 'CFTypeRef ' (aka 'const void * ') is disallowed with ARC 错误:ARC不允许使用指向'CFTypeRef '(又名'const void * ')的Objective-C指针的间接指针

The problem is that attributes shouldn't be a dictionary, it should be a SecKeyRef or CFDataRef. 问题是属性不应该是字典,它应该是SecKeyRef或CFDataRef。 And then cast that back into NSData for the password data copied into it. 然后将其转换回NSData以获取复制到其中的密码数据。

Like so: 像这样:

CFDataRef attributes;
SecItemCopyMatching((__bridge CFDictionaryRef)keychainItemQuery, (CFTypeRef *)&attributes);
NSData *passDat = (__bridge_transfer NSData *)attributes;

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. 这将导致EXEC_BAD_ACCESS,因为resultRef未设置为任何地址并指向内存的某处。

CFDataRef resultRef = nil;

This will fix the error. 这将解决错误。

Need to change attributes to &attributes 需要将attributes更改为&attributes

CFDataRef attributes;
SecItemCopyMatching((__bridge CFDictionaryRef) keychainItemQuery,  ( CFTypeRef*) &attributes);
NSData* passDat=(__bridge_transfer NSData*) attributes;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM