How to convert CFStringRef to NSString?

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


        
相关标签:
8条回答
  • 2020-12-07 09:02

    You have to cast it:

    CFStringRef CFstringFileName=(__bridge CFStringRef)NSstringFileName;
    
    0 讨论(0)
  • 2020-12-07 09:03

    NSString and CFStringRef are "Toll free bridged", meaning that you can simply typecast between them.

    For example:

    CFStringRef aCFString = (CFStringRef)aNSString;
    

    works perfectly and transparently. Likewise:

    NSString *aNSString = (NSString *)aCFString;
    

    The previous syntax was for MRC. If you're using ARC, the new casting syntax is as follows:

    NSString *aNSString = (__bridge NSString *)aCFString;
    

    works as well. The key thing to note is that CoreFoundation will often return objects with +1 reference counts, meaning that they need to be released (all CF[Type]Create format functions do this).

    The nice thing is that in Cocoa you can safely use autorelease or release to free them up.

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