8

I am not getting the differences between CFString and NSString in iOS. Can anyone suggest links or proper answers for this. Thanks in advance.

JWWalker
  • 22,385
  • 6
  • 55
  • 76
user2533604
  • 655
  • 1
  • 11
  • 28
  • 3
    just 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
  • 1
    They 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 Answers2

11

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?

Community
  • 1
  • 1
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
  • +1 for that blog.its really nice. – Vineet Singh Aug 16 '13 at 14:27
  • 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
1

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;
Vineet Singh
  • 4,009
  • 1
  • 28
  • 39