Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert CFArrayRef to NSArray? [duplicate]

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!!!!!!

like image 318
user1574429 Avatar asked Sep 05 '12 15:09

user1574429


2 Answers

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).

like image 157
Jody Hagins Avatar answered Nov 16 '22 22:11

Jody Hagins


You should be able to cast it:

CFArrayRef *myLegacyArray = ...

...

NSArray *myArray=[(NSArray *)myLegacyArray copy];
like image 42
El Developer Avatar answered Nov 16 '22 22:11

El Developer