string 如何将 CFStringRef 转换为 NSString?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/640367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:24:23  来源:igfitidea点击:

How to convert CFStringRef to NSString?

cocoastringcfstring

提问by papr

NSString *aNSString;
CFStringRef aCFString;
aCFString = CFStringCreateWithCString(NULL, [aNSString UTF8String], NSUTF8StringEncoding);
aCFString = CFXMLCreateStringByUnescapingEntities(NULL, aCFString, NULL);

How can I get a new NSStringfrom aCFString?

我怎样才能得到一个新NSStringaCFString

回答by NilObject

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

NSString 和 CFStringRef 是“免费桥接”,这意味着您可以简单地在它们之间进行类型转换。

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:

之前的语法是针对 MRC 的。如果您使用的是 ARC,则新的转换语法如下:

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).

也有效。需要注意的关键是 CoreFoundation 通常会返回引用计数为 +1 的对象,这意味着它们需要被释放(所有 CF[Type]Create 格式函数都这样做)。

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

好消息是,在 Cocoa 中,您可以安全地使用 autorelease 或 release 来释放它们。

回答by clearlight

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

如果您在 Mac OS X/Objective C 的最新版本中使用 ARC,那真的很简单:

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.

但是,当您尝试免费将 CFString 桥接到 NSString 并提供自动将其包装在 CFBridgingRelease() 中时,Xcode 会很高兴地警告您,如果您单击该选项,您可以接受并让它自动为您插入包装器。

回答by Martin Cote

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

它们是等价的,所以你可以只转换 CFStringRef:

NSString *aNSString = (NSString*)aCFString;

For more info, see Toll-Free Bridged Types.

有关详细信息,请参阅免费桥接类型

回答by gavinbeatty

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.

实际上,一般而言,您不应该在 Core Foundation 对象上使用 Cocoa 保留、释放、自动释放。如果您正在使用垃圾收集(目前仅在 Mac OS X 上),那些保留、释放、自动释放调用都是空操作。因此内存泄漏。

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

来自苹果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:

了解 Core Foundation 和 Cocoa 之间的不对称性很重要——其中保留、释放和自动释放是无操作的。例如,如果您使用 release 或 autorelease 平衡了 CFCreate...,您将在垃圾收集环境中泄漏对象:

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.

相反,使用 CFRelease 释放您之前使用 retain 保留的对象将导致引用计数下溢错误。



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

PS:似乎无法评论 Peter Hosey 的回答 - 抱歉不必要地添加了我自己的回答。

回答by Peter Hosey

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 CFStringCreateWithCStringmessage, which is one fewer thing you need to release later. (CF uses Createwhere Cocoa uses alloc, so either way, you would have needed to release it.)

我要补充的是,您不仅可以仅通过类型转换就从 CFString 转到 NSString,而且它也可以以其他方式工作。您可以删除CFStringCreateWithCString消息,这是您稍后需要发布的另一件事。(CF 使用CreateCocoa 使用的地方alloc,所以无论哪种方式,你都需要释放它。)

The resulting code:

结果代码:

NSString *escapedString;
NSString *unescapedString = [(NSString *) CFXMLCreateStringByUnescapingEntities(NULL, (CFStringRef) escapedString, NULL) autorelease];

回答by dloomb

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.

我遇到了 ARC 和 CFStrings 保留计数的问题。使用 NilObjects 回答并稍作调整对我来说效果很好。我刚刚添加了保留,例如。

CFStringRef cfstringRef = (__bridge_retained  CFStringRef)aNsString;

回答by Vincent

You have to cast it:

你必须投它:

CFStringRef CFstringFileName=(__bridge CFStringRef)NSstringFileName;

回答by vualoaithu

Youcan use :With CFStringRef idc;

您可以使用 :With CFStringRef idc;

NSString *sId = [NSString stringWithFormat:@"%@", (NSString*)idc];