Using macros for string concatenation - objective-c

I want to create macro that receives 2 arguments: NSString and ObjCBool and returns NSString.
I'm not familiar a lot with macros, anyway this is what I did so far:
#define fooOne(url)\
#"1111111" url
#define fooTwo(url)\
#"2222222" url
#define root(url, flag)\
if(!flag)fooOne(url)\
else fooTwo(url)
Here I have 2 problems:
1) when I call fooOne as:
NSString *url = #"zzz";
NSLog(#"%#", fooOne(url));
// expected log should be: "1111111 zzz"
I get error: Expected ')'
2) When I call root(url, flag) as:
BOOL flag = YES;
NSString *url = #"zzz";
NSLog(#"%#", root(url, flag));
// expected log should be: "2222222 zzz"
I get an error: Expected expression
please help,

Compile-time string concatenation only works with string literals. At compile time #"hello, " #"world" is combined into a new string literal #"hello, world". This doesn't work with strings contained in variables — #"blabla" url is not a valid expression, even if url contains a string literal at runtime. You would have to call your macros with the literal inside: fooOne(#"zzz"), which would expand to #"blabla" #"zzz".
If you want to combine strings at runtime, you'll need to use +[NSString stringWithFormat:], or append the strings.

Related

objC Preprocessor NSString Macro

I have a problem to create a preprocessor macro function, that concatenates two Strings and "return" a NSString (#"...") value.
Here is what I tried:
#define ObjectKeyMake(NAME) #"com.test.##NAME"
if I print the result from a call I get:
NSLog(#"%#", ObjectKeyMake(foo)); // com.test.##NAME
so my question is: How can i concatenate 2 Strings in a preprocessor macro and "return" a NSString (#"..") ?
and no I can't use #define ObjectKeyMake(NAME) [#"com.test." stringByAppendingString: NAME] because i need a compile-time constant.
You can take advantage of the fact that the compiler combines string literals that are next to each other, like this:
NSString* greeting = #"Hello, " "world";
The macro implementation would look like this:
#define ObjectKeyMake(NAME) (#"com.test." #NAME)
#define ObjectKeyMake(NAME) #"com.test."#NAME

define String concat ios

I have a #define hhh "uu" then I want to concat string using the following method:
NSString *str [NSString stringWithFormat#"%#%#",hhh,"ii"];
but I'm getting a compilation error.
First problem: you are missing the colon : after stringWithFormat. Second problem: you are missing the assignment operator between variable name str and initialization expression.
Third problem: the second argument to the format string #"%#%#" is a plain standard C string (a.k.a., char*), where the format string calls for an object (a.k.a., NSString). Prepend a # to the string literal:
NSString* str = [NSString stringWithFormat: #"%#%#",hhh,#"ii"];
(Edit Assumption wrong, I overlooked the define: I assume here, that hhh is an object reference, e.g., id, NSString* or something).
Since hhh is a plain string, too, you should specify %s as placeholder in the format string:
NSString* str = [NSString stringWithFormat: #"%s%#",hhh,#"ii"];
as was recommended by #sch.

How to get inout to an NSString?

How to get input from an NSString as scanf ("%#", &str); doesn't work?
scanf will read into a C string and not into a NSString (as far as I know). So, to do what you're trying to do you need to first read your input into a C string (i.e. str) and then make that into an NString as follows
myString = [NSString stringWithUTF8String:str];
By the way, you don't need to pass the address of str i.e. &str if str is an array. Simply do:
scanf("%s",str);

Possible to use variables and/or parameters with NSLocalizedString?

I have tried using a variable as an input parameter to NSLocalizedString, but all I am getting back is the input parameter. What am I doing wrong? Is it possible to use a variable string value as an index for NSLocalized string?
For example, I have some strings that I want localized versions to be displayed. However, I would like to use a variable as a parameter to NSLocalizedString, instead of a constant string. Likewise, I would like to include formatting elements in the parameter for NSLocalizedString, so I would be able to retrieved a localized version of the string with the same formatting parameters. Can I do the following:
Case 1: Variable NSLocalizedstring:
NSString *varStr = #"Index1";
NSString *string1 = NSLocalizedString(varStr,#"");
Case 2: Formatted NSLocalizedString:
NSString *string1 = [NSString stringWithFormat:NSLocalizedString(#"This is an %#",#""),#"Apple"];
(Please note that the variable can contain anything, not just a fixed set of strings.)
Thanks!
If what you want is to return the localized version of "This is an Apple/Orange/whatever", you'd want:
NSString *localizedVersion = NSLocalizedString(([NSString stringWithFormat:#"This is an %#", #"Apple"]), nil);
(I.e., the nesting of NSLocalizedString() and [NSString stringWithFormat:] are reversed.)
If what you want is the format to be localized, but not the substituted-in value, do this:
NSString *finalString = [NSString stringWithFormat:NSLocalizedString(#"SomeFormat", nil), #"Apple"];
And in your Localizable.strings:
SomeFormat = "This is an %#";
I just want to add one very helpful definition which I use in many of my projects.
Inspired by androids possibility, I've added this function to my header prefix file:
#define NSLocalizedFormatString(fmt, ...) [NSString stringWithFormat:NSLocalizedString(fmt, nil), __VA_ARGS__]
This allows you to define a localized string like the following:
"ExampleScreenAuthorizationDescriptionLbl"= "I authorize the payment of %# to %#.";
and it can be used via:
self.labelAuthorizationText.text = NSLocalizedFormatString(#"ExampleScreenAuthorizationDescriptionLbl", self.formattedAmount, self.companyQualifier);
For swift :
let myString = String(format: NSLocalizedString("I authorize the payment of %d ", comment: ""), amount)
extension String {
public var localizedString: String {
return NSLocalizedString(self, comment: "")
}
public func localizedString(with arguments: [CVarArg]) -> String {
return String(format: localizedString, arguments: arguments)
}
}
Localizable.string:
"Alarm:Popup:DismissOperation:DeviceMessage" = "\"%#\" will send position updates on a regular basis again.";
"Global:Text:Ok" = "OK";
Usage:
let message = "Alarm:Popup:DismissOperation:DeviceMessage".localizedString(with: [name])
and
let title = "Global:Text:Ok".localizedString
It turns out that a missing target entry is to blame. Just checking that my current build target includes the Localizable.string file solved the problem!
If you have more than one variable in your localized string can you use this solution:
In Localizable.strings
"winpopup" = "#name# wins a #type# and get #points# points(s)";
And use stringByReplacingOccurrencesOfString to insert the values
NSString *string = NSLocalizedString(#"winpopup", nil); //"#name# wins a #type# and get #points# points(s)"
NSString *foo = [string stringByReplacingOccurrencesOfString:#"#name#" withString:gameLayer.turn];
NSString *fooo = [foo stringByReplacingOccurrencesOfString:#"#type#" withString:winMode];
NSString *msg = [fooo stringByReplacingOccurrencesOfString:#"#points#" withString:[NSString stringWithFormat:#"%i", pkt]];
NSLog(#"%#", msg);
Your ideas should work. But if you are getting back the input parameter, that means that the input parameter was not found as a key in your Localizable.strings file. Check the syntax and location of that file.
This works for me:
NSMutableString *testMessage = [NSMutableString stringWithString:NSLocalizedString(#"Some localized text", #"")];
testMessage = [NSMutableString stringWithString:[testMessage stringByAppendingString:someStringVariable]];

Objective C error: Passing argument 1 of 'setStringValue:' from incompatible pointer type

Ok here is part of the code that is causing the error:
char charlieReturn[10000];
charlieReturn[10000] = system("osascript /applications/jarvis/scripts/getTextCharlieResponce.scpt");
self.charlieOutput.stringValue = charlieReturn;
The getTextCharlieResponce.scpt returns something like this: "Hi my name is charlie" and maybe sometimes it will be longer than that. The script returns it plain text. I need help FAST!
Thanks in advance! :D
Elijah
Unfortunately there are many problems in your code.
The C function system does not return the standard output of the script as char*.
Even with a function which returns char*, you can't assign it to a array of char as you did:
char* str="aeiou";
char foo[100];
foo[100]=str; /* doesn't work */
foo=str; /* doesn't work either */
Cocoa's string class, NSString*, is not a C string char*, although you can easily convert between the two:
NSString* str=[NSString stringWithUTF8String:"aeiou"];
If you want a string out of a call to an Apple script, you need to do the following:
Prepare an NSAppleScript:
NSDictionary* errorDict;
NSAppleScript* script=[[NSAppleScript alloc]
initWithContentsOfURL:[NSURL fileURLWithPath:#"path/to/script" ]
error:&errorDict];
Execute and get a reply:
NSAppleEventDescriptor* desc=[script executeAndReturnError:&errorDict];
NSString* result=[desc stringValue];
Release the script:
[script release];
Learn C & Objective-C and have fun!
You're trying to assign a character array to what is presumably an NSString. Try this instead:
self.charlieOutput.stringValue = [NSString stringWithUTF8String:charlieReturn];