This question already has answers here:
Comma in C/C++ macro
(8 answers)
Closed 5 years ago.
Assuming I have a macro like this
#define A(x) NSLog(#"%#", x)
how can I call it and format the string in the macro argument like this
A([NSString stringWithFormat:#"Random string with number %d", 5]);
I am getting error too many arguments provided to function-like macro invocation and the error marker points to the comma, which makes sense, since I assume that is there the preprocessor splits the arguments, since it does not know the context that it is a selector call. Is it even possible to do this?
As the linked question's answer says, the solution lies in using an extra pair of paranthese at the call site:
A(([NSString stringWithFormat:#"Random string with number %d", 5]));
works.
Related
This question already has answers here:
Pass all arguments of a method into NSLog
(2 answers)
Closed 8 years ago.
I know that NSStringFromSelector(_cmd) gives the name of the current method. I use that in a debugging macro to print what method I'm in. I would like to also get the method's arguments as strings so I can print them too. I'm thinking of something like *argv[] in C.
Is there any built-in facility for that?
Yes, c++ (and Objective-C) support va_list.
Do something like this:
- (void)do:(va_list)args
{
// do something with all your args
}
And use it like this:
[self do:#"foo", #"bar"]
This question already has answers here:
Caret in objective C
(3 answers)
Closed 9 years ago.
There is a piece of code like
typedef void (^SignIn) (NSString *email, NSString *password);
What does the ^ mean before SignIn? Is this Objective-C specific usage?
It's the syntax for blocks.
That typedef declares SignIn to mean a block which takes two NSString* arguments and returns void (i.e. nothing).
It is a block.
For a guide to understanding blocks, see this tutorial
Unless, you already know what a block is, and you just didn't know what the caret was for.
This question already has an answer here:
Print out the variable name objective-C
(1 answer)
Closed 9 years ago.
Is there a way to ask a pointer / variable for its name as a string?
i.e...
NSNumber* aNumber;
int anInt;
NSString* name = aFunctionThatDoesWhatIAskedFor(aNumber);
NSLog(#"%#",name); //should print "aNumber";
name = aFunctionThatDoesWhatIAskedFor(anInt);
NSLog(#"%#",name); //should print "anInt";
define this macro
#define nameOfVariable(x) NSLog( #"%s",#x)
use this macro
nameOfVariable(aNumber);
nameOfVariable(anInt);
Explanation:
Preceding parameter name by # is known as Stringification. You can use the ‘#’ operator to stringify the variable argument or to paste its token with another token.
Sometimes you may want to convert a macro argument into a string
constant. Parameters are not replaced inside string constants, but you
can use the #' preprocessing operator instead. When a macro parameter
is used with a leading#', the preprocessor replaces it with the
literal text of the actual argument, converted to a string constant.
Unlike normal parameter replacement, the argument is not
macro-expanded first. This is called stringification.
In most programming languages objects don't have names.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Warning: “format not a string literal and no format arguments”
I have the following line of code that is in my app that a Developer worked on. I am learning the basics of Objective C and as I was updating the App to be iPhone 5 compatible, I see the following warning (I did not change his code) Format String is not a literal string (potentially insecure). The code is as follows;
self.progressHud.labelText = [NSString stringWithFormat:message];
I don't know exactly what this means and don't want to upload anything that can either be a security issue or get rejected by Apple. Any and all help is appreciated from you all.
Use the following Lines:
self.progressHud.labelText = [NSString stringWithFormat:#"%#", message];
In objective C, this line get the values from any format such as int,float etc to display the Label. because UILabel and IBOutlet Elements only display the NSString values.
However, if you don't need to create a string with multiple variables, it would be more efficient to simply use:
self.progressHud.labelText = message;
This question already has answers here:
Semicolon after the method name in Objective-C implementation file
(6 answers)
Closed 9 years ago.
I'm trying to figure out why gcc is OK with the following:
- (void) methodname: (id) sender;
{
// do stuff
}
Notice what's wrong here: There is a semicolon that should not be there.
I'm also finding that execution of the method is having bizarre results.
Anybody know what effect the semicolon is supposed to have or not have here?
Thanks.
The semi-colon is optional in the context of the #implementation. Some teams standardize on requiring it, some don't.
In general ; makes it a function prototype. But the compiler must be smart enough to ignore it in this case. I don't think it's the ; that's causing your odd issues but I'm not sure.