Possible Duplicate:
How to make an CFArrayRef to an NSMutableArray
I know there is so many info about this questions, but every single answers are "this is easy. you can do cast as toll-free bridged.", and then there is no mention of actual source code. I know the mean of toll-free bridged and cast. I wanna know the exact source code "How to convert CFArrayRef to NSArray?" . Anyone please!!!!!!
Well, first you have to know if you are using ARC or not, because the rules change a bit. Next, you need to know what you actually want to do with your cast. Do you just want to use the value, or transfer ownership?
I will assume ARC, because, IMO, all new code really should be using ARC (and ARC is where the casting problems are more prevalent).
CFArrayRef someArrayRef = ...;
NSArray *array = (__bridge NSArray*)someArrayRef;
The above code casts the CF reference to an NSArray*
that can be used in the current context. No transfer of ownership has taken place. someArrayRef
still holds its reference, and you still must manually release someArrayRef
or it will leak.
CFArrayRef someArrayRef = ...;
NSArray *array = CFBridgingRelease(someArrayRef);
In this code, you not only get a cast, but a transfer of ownership. someArrayRef
now no longer holds the reference, so it does not need to be manually released. Instead, when array
releases, the object will dealloc (assuming no other references in other places).
You should be able to cast it:
CFArrayRef *myLegacyArray = ...
...
NSArray *myArray=[(NSArray *)myLegacyArray copy];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With