views:

553

answers:

1

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

Update: 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.

A: 

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.

mouviciel
This throws the error I described above.
Randall
How is defined your `ConstantFromCFStringRef`?
mouviciel
I tried `NSString * myConstant = (NSString *)CFStringRef;` but that throws the above error.I attempted to use CFStringGetCStringPtr and CFStringGetCString, but those also failed, so I am now attempting to write this to avoid having to expose the constants at all (and where I do have to expose them, I'm using my own constants and a couple of private methods to convert them for use by the underlying framework).
Randall