I must use a union containing a class pointer, but with ARC on, and as very well explained here: __unsafe_unretained NSString struct var you have to set the field __unsafe_unretained.
That means, if I understand it well, you must manage yourself its lifecycle.
For example:
typedef union FOO {
char __char;
__unsafe_unretained NSMutableArray * __array;
__unsafe_unretained BarClass * __bar;
}
If I do something like:
FOO * foo = malloc(sizeof(FOO));
foo.__bar = [[BarClass alloc] init];
... // I have fun with foo.__bar
[foo.__bar release] // this was before ARC and does not work anymore
free(foo);
How do I release foo.__bar? Because with ARC I cannot call releaseor autorelease anymore?
The easiest way is to use a
__bridgecast andCFRelease:Some other notes:
Don’t use the names
__char,__array, and__bar. All identifiers that begin with two consecutive underscores are reserved by the C standard.You need to retain the object too, before the end of the statement that creates it, because at the end of that statement, ARC will release it. The easiest way to do that is by abusing
CFBridgingRetain:You might be better off just turning
FOOinto an Objective-C class so that it can hold strong references.