How to convert CFStringRef to NSString?

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


        
8条回答
  •  暗喜
    暗喜 (楼主)
    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.

提交回复
热议问题