oc cfstringreff和nsstring的区别

他的最新文章
他的热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)他的最新文章
他的热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)CFStringRef和NSString怎么相互转换_百度知道
CFStringRef和NSString怎么相互转换
我有更好的答案
NSString *strNS = @&&;CFStringRef strRef = (__bridge CFStringRef)strNS;strNS = (__bridge NSString *)strR
采纳率:11%
为您推荐:
其他类似问题
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。cocoa - How to convert CFStringRef to NSString? - Stack Overflow
Join Stack Overflow to learn, share knowledge, and build your career.
or sign in with
NSString *aNSS
CFStringRef aCFS
aCFString = CFStringCreateWithCString(NULL, [aNSString UTF8String], NSUTF8StringEncoding);
aCFString = CFXMLCreateStringByUnescapingEntities(NULL, aCFString, NULL);
How can I get a new NSString from aCFString?
65k99330517
2,12252437
NSString and CFStringRef are "Toll free bridged", meaning that you can simply typecast between them.
For example:
CFStringRef aCFString = (CFStringRef)aNSS
works perfectly and transparently. Likewise:
NSString *aNSString = (NSString *)aCFS
The previous syntax was for MRC. If you're using ARC, the new casting syntax is as follows:
NSString *aNSString = (__bridge NSString *)aCFS
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.
9,86064186
9,64412843
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.
7,85493555
They are equivalent, so you can just cast the CFStringRef:
NSString *aNSString = (NSString*)aCFS
For more info, see .
19.7k126596
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 :
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.
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 *escapedS
NSString *unescapedString = [(NSString *) CFXMLCreateStringByUnescapingEntities(NULL, (CFStringRef) escapedString, NULL) autorelease];
35.8k41152249
86.2k13181344
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)aNsS
You have to cast it:
CFStringRef CFstringFileName=(__bridge CFStringRef)NSstringFileN
Youcan use :With CFStringR
NSString *sId = [NSString stringWithFormat:@"%@", (NSString*)idc];
4,55212545
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabledCFString | Apple Developer Documentation
FrameworkCore FoundationOverviewCFString provides a suite of efficient string-manipulation and string-conversion functions. It offers seamless Unicode support and facilitates the sharing of data between Cocoa and C-based programs. CFString objects are immutable—use
to create and manage a string that can be changed after it has been created.CFString has two primitive functions,
and , that provide the basis for all other functions in its interface. The CFStringGetLength function returns the total number (in terms of UTF-16 code pairs) of characters in the string. The CFStringGetCharacterAtIndex function gives access to each character in the string by index, with index values starting at 0. CFString provides functions for finding and comparing strings. It also provides functions for reading numeric values from strings, for combining strings in various ways, and for converting a string to different forms (such as encoding and case changes). A number of functions, for example CFStringFindWithOptions, allow you to specify a range over which to operate within a string. The specified range must not exceed the length of the string. Debugging options may help you to catch any errors that arise if a range does exceed a string’s length.Like other Core Foundation types, you can hash CFStrings using the
function. You should never, though, store a hash value outside of your application and expect it to be useful if you read it back in later (hash values may change between different releases of the operating system).CFString is “toll-free bridged” with its Cocoa Foundation counterpart, . 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
for more information on toll-free bridging.TopicsCreating a CFStringSearching StringsComparing StringsAccessing CharactersWorking With HyphenationWorking With EncodingsGetting Numeric ValuesGetting String PropertiesString File System RepresentationsGetting Paragraph BoundsManaging SurrogatesData TypesConstantsSee AlsoOpaque Types}

我要回帖

更多关于 cfstringref 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信