开发者

Create constant NSString from constant CFStringRef

I am attempting to create a Cocoa Framework that is an object-oriented wrapper around a procedural framework written in the Core Foundation. The procedural framework has a number of constants that are all defined as extern CFStringRef constantName.

How should I create NSString constants that directly map to the procedural constants so that in my framework I can simply cast the NSString constant to the CFStringRef constant within the framework such that the developer using my framework does not need to be aware of the casts himself.

Every thing that I have tried results in the compiler error Initializer element is not constant. This is the pattern I would like to use:

Constants.h:

extern NSString * myConstant

Constants.m:

#import "Constants.h"
NSString *myConstant = ConstantFromCFStringRef;

I am successfully declaring constant values with NSString in Constants.m using NSString *aConstant = @"someStringLiteral" but in this case, I want to have the same value开发者_如何转开发 as the CFStringRefs that I cannot ignore.


Have you tried:

NSString * myconstant = (NSString *)constantName;

You can find the same answer here: How to convert CFStringRef to NSString? and there: CocoaDev: CFStringRef.

Indeed Apple has already solved your problem. This is called Toll Free Bridging.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜