How to convert CFStringRef to NSString?

前端 未结 8 1863
借酒劲吻你
借酒劲吻你 2020-12-07 08:37
NSString *aNSString;
CFStringRef aCFString;
aCFString = CFStringCreateWithCString(NULL, [aNSString UTF8String], NSUTF8StringEncoding);
aCFString = CFXMLCreateStringB         


        
相关标签:
8条回答
  • 2020-12-07 08:39

    If you're using ARC in recent versions of Mac OS X/Objective C, it's real easy:

    NSString *happyString = (NSString *)CFBridgingRelease(sadString);
    

    However, Xcode will happily warn you when you try to toll free bridge CFString to NSString and offer to automatically wrap it in CFBridgingRelease(), which you can accept and let it automatically insert the wrapper for you if you click the option.

    0 讨论(0)
  • 2020-12-07 08:39

    I'll add that not only can you go from CFString to NSString with only a type-cast, but it works the other way as well. You can drop the CFStringCreateWithCString message, which is one fewer thing you need to release later. (CF uses Create where Cocoa uses alloc, so either way, you would have needed to release it.)

    The resulting code:

    NSString *escapedString;
    NSString *unescapedString = [(NSString *) CFXMLCreateStringByUnescapingEntities(NULL, (CFStringRef) escapedString, NULL) autorelease];
    
    0 讨论(0)
  • 2020-12-07 08:44

    Actually, you shouldn't use Cocoa retain, release, autorelease on Core Foundation objects in generality. If you're using Garbage Collection (only on Mac OS X for now), those retain, release, autorelease calls are all no-ops. Hence memory leaks.

    From Apple http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/GarbageCollection/Articles/gcCoreFoundation.html:

    It is important to appreciate the asymmetry between Core Foundation and Cocoa—where retain, release, and autorelease are no-ops. If, for example, you have balanced a CFCreate… with release or autorelease, you will leak the object in a garbage collected environment:

    NSString *myString = (NSString *)CFStringCreate...(...);
    // do interesting things with myString...
    [myString release]; // leaked in a garbage collected environment
    

    Conversely, using CFRelease to release an object you have previously retained using retain will result in a reference count underflow error.


    PS: can't seem to comment on Peter Hosey's answer - sorry for adding my own unnecessarily.

    0 讨论(0)
  • 2020-12-07 08:47

    I was having an issue with ARC and the retain count of CFStrings. Using NilObjects answer with a slight tweak worked perfect for me. I just added retained eg.

    CFStringRef cfstringRef = (__bridge_retained  CFStringRef)aNsString;
    
    0 讨论(0)
  • 2020-12-07 08:59

    Youcan use :With CFStringRef idc;

    NSString *sId = [NSString stringWithFormat:@"%@", (NSString*)idc];
    
    0 讨论(0)
  • 2020-12-07 09:02

    They are equivalent, so you can just cast the CFStringRef:

    NSString *aNSString = (NSString*)aCFString;
    

    For more info, see Toll-Free Bridged Types.

    0 讨论(0)
提交回复
热议问题