Discussion:
Using CFSTR() with const char * variable
Carl Hoefs
2015-06-06 03:02:42 UTC
Permalink
If I use CFSTR() in the following way:
CFStringRef mystr = CFSTR( "This is a string" );
there is no problem.

However, if I use a variable instead of “string” Xcode flags this as an error:
const char *mystring = "This is a string";
CFStringRef mystr = CFSTR( mystring );
^ <— Expected ")"

In CFString.h, CFSTR is defined as:
#define CFSTR(cStr) __CFStringMakeConstantString("" cStr "")

Is it possible to use CFSTR() with a const char * variable?

Xcode 6.3.2
-Carl


_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.net

This email sent to ***@ml-
Roland King
2015-06-06 03:12:27 UTC
Permalink
CFSTR() documentation …

cStr
A constant C string (that is, text enclosed in double-quotation marks) from which the string is to be created.
so no. If you look to see how that macro expands you can see why.
Post by Carl Hoefs
CFStringRef mystr = CFSTR( "This is a string" );
there is no problem.
const char *mystring = "This is a string";
CFStringRef mystr = CFSTR( mystring );
^ <— Expected ")"
#define CFSTR(cStr) __CFStringMakeConstantString("" cStr "")
Is it possible to use CFSTR() with a const char * variable?
Xcode 6.3.2
-Carl
_______________________________________________
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
https://lists.apple.com/mailman/options/cocoa-dev/rols%40rols.org
_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.
Ken Thomases
2015-06-06 03:17:24 UTC
Permalink
Post by Carl Hoefs
CFStringRef mystr = CFSTR( "This is a string" );
there is no problem.
const char *mystring = "This is a string";
CFStringRef mystr = CFSTR( mystring );
^ <— Expected ")"
#define CFSTR(cStr) __CFStringMakeConstantString("" cStr "")
Is it possible to use CFSTR() with a const char * variable?
No. As you can see from the quoted macro definition, it relies on concatenation of adjacent string literals.

You should use an appropriate CFStringCreate…() function to create a CFString from the C string. Or just create a CFString or NSString literal from the start.

Be careful with CFStringCreateWithCStringNoCopy(…, kCFAllocatorNull), which you might be tempted to use. Only use that with pointer to static storage. The mystring variable in your example does point to static storage, but could be changed to point to something else (it's a pointer to const char, not a const pointer to const char).

Something like this is _not_ safe:

const char mystring[] = "This is a string";

That is auto-lifetime storage allocated on the heap, which will be deallocated at end of scope.

This would be safe:

static const char mystring[] = "This is a string";

Regards,
Ken


_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.net

This email sent to ***@ml-
Carl Hoefs
2015-06-06 04:12:54 UTC
Permalink
Post by Ken Thomases
Post by Carl Hoefs
CFStringRef mystr = CFSTR( "This is a string" );
there is no problem.
const char *mystring = "This is a string";
CFStringRef mystr = CFSTR( mystring );
^ <— Expected ")"
#define CFSTR(cStr) __CFStringMakeConstantString("" cStr "")
Is it possible to use CFSTR() with a const char * variable?
No. As you can see from the quoted macro definition, it relies on concatenation of adjacent string literals.
You should use an appropriate CFStringCreate…() function to create a CFString from the C string. Or just create a CFString or NSString literal from the start.
Be careful with CFStringCreateWithCStringNoCopy(…, kCFAllocatorNull), which you might be tempted to use. Only use that with pointer to static storage. The mystring variable in your example does point to static storage, but could be changed to point to something else (it's a pointer to const char, not a const pointer to const char).
const char mystring[] = "This is a string";
That is auto-lifetime storage allocated on the heap, which will be deallocated at end of scope.
static const char mystring[] = "This is a string”;
Okay, got it. (BTW, it’s probably just me, but the entire set of CF* functions seems to be a bit oddly designed, no?)

Thanks Ken and Roland!
-Carl


_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.net
d***@gmail.com
2015-06-06 04:43:32 UTC
Permalink
Sent from my iPhone
Post by Carl Hoefs
Okay, got it. (BTW, it’s probably just me, but the entire set of CF* functions seems to be a bit oddly designed, no?)
They might seem so at first but they're pretty clever and well designed as an Object Oriented C.
They're also designed in many ways for implementing the guts of Foundation and other Objective-C classes in a consistent and performant way. Functions are faster that method lookup.
_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.n
Steve Christensen
2015-06-07 01:39:36 UTC
Permalink
In my prefix file that is used to build precompiled headers, I include the following after #import-ing all the framework headers. It replaces the standard definition with a version that doesn't insert quotes around an unquoted string literal.

#undef CFSTR//(cStr)
#ifdef __CONSTANT_CFSTRINGS__
#define CFSTR(cStr) ((CFStringRef) __builtin___CFStringMakeConstantString(cStr))
#else
#define CFSTR(cStr) __CFStringMakeConstantString(cStr)
#endif
Post by Carl Hoefs
CFStringRef mystr = CFSTR( "This is a string" );
there is no problem.
const char *mystring = "This is a string";
CFStringRef mystr = CFSTR( mystring );
^ <— Expected ")"
#define CFSTR(cStr) __CFStringMakeConstantString("" cStr "")
Is it possible to use CFSTR() with a const char * variable?
Xcode 6.3.2
-Carl
_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.net

This email sent to ***@ml-in.narkive
Jean-Daniel Dupas
2015-06-08 05:16:31 UTC
Permalink
That’s not going to work.

__builtin___CFStringMakeConstantString is a special compiler function that requires a constant string to work in the first place, as it tells the compiler to generate CFString literal.

I doubt the compiler will accept anything else as parameter.
Post by Steve Christensen
In my prefix file that is used to build precompiled headers, I include the following after #import-ing all the framework headers. It replaces the standard definition with a version that doesn't insert quotes around an unquoted string literal.
#undef CFSTR//(cStr)
#ifdef __CONSTANT_CFSTRINGS__
#define CFSTR(cStr) ((CFStringRef) __builtin___CFStringMakeConstantString(cStr))
#else
#define CFSTR(cStr) __CFStringMakeConstantString(cStr)
#endif
Post by Carl Hoefs
CFStringRef mystr = CFSTR( "This is a string" );
there is no problem.
const char *mystring = "This is a string";
CFStringRef mystr = CFSTR( mystring );
^ <— Expected ")"
#define CFSTR(cStr) __CFStringMakeConstantString("" cStr "")
Is it possible to use CFSTR() with a const char * variable?
Xcode 6.3.2
-Carl
_______________________________________________
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
https://lists.apple.com/mailman/options/cocoa-dev/mailing%40xenonium.com
_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.net

This email sent to ***@ml-

Loading...