I'm trying to create a macro definition that can emit C++ or Objective-C depending on context but can't seem to construct an NSString inside a macro easily. The C++ version is simple because it uses regular strings, but making one that emits NSString is proving tricky:
#define FOO(x) bar(@##x)
The intended result is to convert a string argument to an NSString argument by prefixing with @
:
FOO("x")
// => bar(@"x")
What I get instead is an error that prevents compilation:
Pasting formed '@"x"', an invalid preprocessing token
NSString *myNSString = [NSString stringWithFormat:@"test"]; NSString *myRetainedNSString = [[NSString alloc] initWithFormat:@"test"]; Or if you need an editable string: NSMutableString *myMutableString = [NSMutableString stringWithFormat:@"test"];
A static, plain-text Unicode string object which you use when you need reference semantics or other Foundation-specific behavior.
eg: const int age = 10; A string literal is a expression like @"" . The compiler will replace this with an instance of NSString . A string constant is a read-only pointer to NSString .
NSString *x = @"text";
Equals:
NSString *x = CFSTR("text");
PS NSString* and CFStringRef and __CFString* and also NSCFStringRef are all the same: Toll-Free Bridged Types
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With