Discussion:
NSString equivalent of CFSTR macro?
Rick Mann
2017-12-04 22:47:24 UTC
Permalink
I have to use some C header file that #defines some string constants. Is there an equivalent to CFSTR() that constructs NSString literals? E.g.,


#define NSSTR(s) (@ ## s) <-- magic; this doesn't work
#define kSomeCStringConstant "foo"
...
NSSTR(kSomeCStringConstant)

TIA,
--
Rick Mann
***@latencyzero.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-in.narkive.net
Ben Kennedy
2017-12-04 22:51:30 UTC
Permalink
Post by Rick Mann
#define kSomeCStringConstant "foo"
...
NSSTR(kSomeCStringConstant)
You're close. The preprocessor is removing the quotation marks, breaking the syntax. You don't need to token paste; simply remove the '##' from the NSSTR() def.

#define NSSTR(s) (@ s)

-b

_______________________________________________

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.net
Rick Mann
2017-12-04 22:53:46 UTC
Permalink
I tried that. It doesn't work.
Post by Ben Kennedy
Post by Rick Mann
#define kSomeCStringConstant "foo"
...
NSSTR(kSomeCStringConstant)
You're close. The preprocessor is removing the quotation marks, breaking the syntax. You don't need to token paste; simply remove the '##' from the NSSTR() def.
-b
--
Rick Mann
***@latencyzero.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-in.narkive.net
Ben Kennedy
2017-12-04 22:56:35 UTC
Permalink
Post by Rick Mann
I tried that. It doesn't work.
Weird. I just tried it here, using your exact example, and it worked fine under Xcode 9.2. (I slapped it into my iOS app's application:didFinishLaunchingWithOptions: as a quick and dirty test.)

#define NSSTR(s) (@ s)
#define kSomeCStringConstant "foo"
NSLog(@"foo is %@", NSSTR(kSomeCStringConstant));

Do you have some unusual circumstances in your build environment?

b

_______________________________________________

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.net
Rick Mann
2017-12-04 22:58:29 UTC
Permalink
Ah, nvm, user error. I'm an idiot. It's not a #defined string. It's:

static const char* const kSomeCStringConstant = "foo";
Post by Ben Kennedy
Post by Rick Mann
I tried that. It doesn't work.
Weird. I just tried it here, using your exact example, and it worked fine under Xcode 9.2. (I slapped it into my iOS app's application:didFinishLaunchingWithOptions: as a quick and dirty test.)
#define kSomeCStringConstant "foo"
Do you have some unusual circumstances in your build environment?
b
--
Rick Mann
***@latencyzero.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-in.narkive.net
Greg Parker
2017-12-04 22:55:45 UTC
Permalink
Post by Ben Kennedy
Post by Rick Mann
#define kSomeCStringConstant "foo"
...
NSSTR(kSomeCStringConstant)
You're close. The preprocessor is removing the quotation marks, breaking the syntax. You don't need to token paste; simply remove the '##' from the NSSTR() def.
You can also skip the middleman and use `@kSomeCStringConstant`. After the preprocessor runs it will be `@"foo"` which the ObjC compiler is perfectly happy with.
--
Greg Parker ***@apple.com Runtime Wrangler


_______________________________________________

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.net
Rick Mann
2017-12-04 22:56:55 UTC
Permalink
Post by Ben Kennedy
Post by Rick Mann
#define kSomeCStringConstant "foo"
...
NSSTR(kSomeCStringConstant)
You're close. The preprocessor is removing the quotation marks, breaking the syntax. You don't need to token paste; simply remove the '##' from the NSSTR() def.
--
--
Rick Mann
***@latencyzero.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-in.narkive.net
Greg Parker
2017-12-04 23:00:02 UTC
Permalink
Post by Ben Kennedy
Post by Rick Mann
#define kSomeCStringConstant "foo"
...
NSSTR(kSomeCStringConstant)
You're close. The preprocessor is removing the quotation marks, breaking the syntax. You don't need to token paste; simply remove the '##' from the NSSTR() def.
What compiler version are you using?
Is this file compiled as Objective-C?
Is the #define spelled differently than you wrote?
Does anything look wrong with that line in the output of Product > Perform Action > Preprocess ?
--
Greg Parker ***@apple.com Runtime Wrangler


_______________________________________________

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.net
Rick Mann
2017-12-04 23:22:53 UTC
Permalink
Ugh...been doing too much Swift (j/k). The right answer to this question is:

@(kSomeCStringConstant)

This works whether it's a #define or a static const char* const.
Post by Greg Parker
Post by Ben Kennedy
Post by Rick Mann
#define kSomeCStringConstant "foo"
...
NSSTR(kSomeCStringConstant)
You're close. The preprocessor is removing the quotation marks, breaking the syntax. You don't need to token paste; simply remove the '##' from the NSSTR() def.
What compiler version are you using?
Is this file compiled as Objective-C?
Is the #define spelled differently than you wrote?
Does anything look wrong with that line in the output of Product > Perform Action > Preprocess ?
--
--
Rick Mann
***@latencyzero.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-in.narkive.net
Greg Parker
2017-12-05 00:14:49 UTC
Permalink
Post by Rick Mann
@(kSomeCStringConstant)
This works whether it's a #define or a static const char* const.
The downsides are:
1. If the library requires that you use the actual address stored in string variable kSomeCStringConstant then it may fail because @(kSomeCStringConstant) may create a second object at a different address.
2. The compiler is not currently smart enough to optimize @(kSomeCStringConstant) into a constant string object, so it may allocate a new autoreleased string object every time it runs that line.
--
Greg Parker ***@apple.com Runtime Wrangler


_______________________________________________

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.net
Rick Mann
2017-12-05 00:23:46 UTC
Permalink
Post by Rick Mann
@(kSomeCStringConstant)
This works whether it's a #define or a static const char* const.
Yeah, I figured as much. In this case, that's okay. I was just trying to declutter the call site. Thanks!
--
Rick Mann
***@latencyzero.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-in.narkive.net
Alastair Houghton
2017-12-05 11:15:36 UTC
Permalink
Post by Rick Mann
I have to use some C header file that #defines some string constants. Is there an equivalent to CFSTR() that constructs NSString literals? E.g.,
#define kSomeCStringConstant "foo"
...
NSSTR(kSomeCStringConstant)
Why not just do

#define NSSTR(s) ((NSString *)CFSTR(s))

After all, constant NSStrings are the same as constant CFStrings, right?

Kind regards,

Alastair.

--
http://alastairs-place.net

_______________________________________________

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

Loading...