I am not getting the differences between CFString and NSString in iOS. Can anyone suggest links or proper answers for this. Thanks in advance.
-
3just write your Question on google instead for here :) sorry to rude comment but it is very easy and very better expansion your will find on google :) – iPatel Aug 16 '13 at 13:08
-
This might help [CoreFoundation Toll-Free Bridging](https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaEncyclopedia/Toll-FreeBridgin/Toll-FreeBridgin.html) – Jonathan Cichon Aug 16 '13 at 13:08
-
1They are essentially the same thing, just different faces. From the NSString spec: "NSString is “toll-free bridged” with its Core Foundation counterpart, CFStringRef. See [Toll-Free Bridging](http://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaEncyclopedia/Toll-FreeBridgin/Toll-FreeBridgin.html#//apple_ref/doc/uid/TP40010810-CH2) for more information on toll-free bridging." – Hot Licks Aug 16 '13 at 13:09
-
Nice write-up about it: http://ridiculousfish.com/blog/posts/bridge.html – Gabriele Petronella Aug 16 '13 at 13:10
2 Answers
NSString
is the Foundation counterpart of CFString
. This means that they have the exactly the same memory layout in memory.
The reason behind this architecture is historical and goes back to the "collision" between NeXTSTEP and MacOS 9.
Thanks to the toll-free bridging mechanism many CF objects have an interchangeable NS counterpart (other examples are CFArray
/NSArray
, CFData
/NSData
, ...) so from a practical point of you can think of them as the same thing.
Here's a nice write-up on the topic: http://ridiculousfish.com/blog/posts/bridge.html
As extra goodie here's my answer on how to perform bridging in ARC environments: NSString to CFStringRef and CFStringRef to NSString in ARC?

- 1
- 1

- 106,943
- 21
- 217
- 235
-
-
When you have a toll-free bridged class, it isn't "built" on its CF counterpart, it *is* its CF counterpart. They share the same memory layout, and CF even has an isa pointer to let messages get tossed back into ObjC land – CodaFi Aug 17 '13 at 18:14
EDIT : This answer is for non-ARC environment.
As per iOS developer library :
CFString is “toll-free bridged” with its Cocoa Foundation counterpart, NSString. This means that the Core Foundation type is interchangeable in function or method calls with the bridged Foundation object. Therefore, in a method where you see an NSString * parameter, you can pass in a CFStringRef, and in a function where you see a CFStringRef parameter, you can pass in an NSString instance. This also applies to concrete subclasses of NSString. See “Toll-Free Bridged Types” for more information on toll-free bridging
Convert NSString to CFStringRef:
NSString *test = @"Everyone is created equal.";
CFStringRef string = (CFStringRef) test;
Convert CFStringRef to NSString:
NSString *backToNSString = (NSString *) string;

- 4,009
- 1
- 28
- 39
-
1That won't work on ARC. Check out my answer here http://stackoverflow.com/questions/17227348/nsstring-to-cfstringref-and-cfstringref-to-nsstring-in-arc/17256947#17256947 where I explain the proper bridged conversion ;) – Gabriele Petronella Aug 16 '13 at 14:37
-
-
didn't downvote you, but as I said that's the wrong way for casting in ARC – Gabriele Petronella Aug 17 '13 at 12:03
-
-
You'd better specify that then. Nowadays the default is ARC ;) – Gabriele Petronella Aug 17 '13 at 12:15
-
@GabrielePetronella : It depends on us,that do we want to work in ARC environment or not,and while asking this question, user2533604 has not specified that is he working in ARC environment or not. – Vineet Singh Aug 17 '13 at 12:19
-
Exactly, he didn't specify, so you have to make an (explicit) assumption or ask for clarifications to the OP. It costs you a few keystrokes and it makes your answer clearer and more useful to the next guy coming here. – Gabriele Petronella Aug 17 '13 at 12:30
-
1