NSLog - How to print the name of an object?

Consider

NSString *myString = @"Welcome";

NSLog(@"%@",myString);

will print Welcomein the console.

Can I print a string as " myString: Welcome"?

I mean, can I get the name of the object (" myString") along with the value of the object (" Welcome")?

+4
source share
1 answer

Use the following code:

#define stringVariable(x) NSLog( @"%s:%@",#x, x) 

NSString *myString=@"Welcome";

stringVariable(myString); 

Note. . The general principle is that when you put # in front of an argument inside the body of #define, the preprocessor replaces it with the string C of the exact expression passed in the macro. When you pass the variable name, you get that name.

+6
source

Source: https://habr.com/ru/post/1584877/


All Articles