Casting an NSError return to a CFErrorRef return
Question
I have a function that returns an NSError object by reference:
NSData *foo(NSData *foo, NSError *__autoreleasing *outError);
This function uses an API that takes a pointer to storage for a CFErrorRef. I'd like to just pass outError
to the underlying function directly, but I can't figure out the correct combination of declaration keywords and cast keywords to make clang agree with that plan. Is there one?
La solution
If you look at the clang notes for __autoreleasing
it mentions that the magic happens at assignment time, which means that automatic casting can't help here. You need to actually make the assignment using a temporary variable as mentioned in the comments on the original post.
Autres conseils
Try this:
NSError+CFErrorRef.h
@interface NSError (CFErrorRef)
- (CFErrorRef) cferror;
@end
NSError+CFErrorRef.m
#import "NSError+CFErrorRef.h"
@implementation NSError (CFErrorRef)
- (CFErrorRef) cferror
{
CFStringRef domain = (CFStringRef) self.domain;
CFDictionaryRef userInfo = (__bridge CFDictionaryRef) self.userInfo;
return CFErrorCreate(kCFAllocatorDefault, domain, self.code, userInfo);
}
@end
I wrote a quick little unit test to verify everything converted over and it appears to be working correctly. Then you can just perform the following selector on your NSError object
CFErrorRef e = [error cferror];