Using NSLog for debugging
I have the following code snippet in my Xcode:
NSString *digit [[sender titlelabel] text];
NSLog([digit]);
开发者_如何学编程
I tried to build the application and am getting the following warning message for the line NSLog([digit]);
Warning: Format not a string literal and no format arguments
Can you advise me how I can resolve this warning message? What does the message actually mean?
Try this piece of code:
NSString *digit = [[sender titlelabel] text];
NSLog(@"%@", digit);
The message means that you have incorrect syntax for using the digit
variable. If you're not sending it any message - you don't need any brackets.
Use NSLog()
like this:
NSLog(@"The code runs through here!");
Or like this - with placeholders:
float aFloat = 5.34245;
NSLog(@"This is my float: %f \n\nAnd here again: %.2f", aFloat, aFloat);
In NSLog()
you can use it like + (id)stringWithFormat:(NSString *)format, ...
float aFloat = 5.34245;
NSString *aString = [NSString stringWithFormat:@"This is my float: %f \n\nAnd here again: %.2f", aFloat, aFloat];
You can add other placeholders, too:
float aFloat = 5.34245;
int aInteger = 3;
NSString *aString = @"A string";
NSLog(@"This is my float: %f \n\nAnd here is my integer: %i \n\nAnd finally my string: %@", aFloat, aInteger, aString);
Why do you have the brackets around digit
?
It should be
NSLog("%@", digit);
You're also missing an =
in the first line...
NSString *digit = [[sender titlelabel] text];
The proper way of using NSLog, as the warning tries to explain, is the use of a formatter, instead of passing in a literal:
Instead of:
NSString *digit = [[sender titlelabel] text];
NSLog(digit);
Use:
NSString *digit = [[sender titlelabel] text];
NSLog(@"%@",digit);
It will still work doing that first way, but doing it this way will get rid of the warning.
type : BOOL DATA (YES/NO) OR(1/0)
BOOL dtBool = 0;
OR
BOOL dtBool = NO;
NSLog(dtBool ? @"Yes" : @"No");
OUTPUT : NO
type : Long
long aLong = 2015;
NSLog(@"Display Long: %ld”, aLong);
OUTPUT : Display Long: 2015
long long veryLong = 20152015;
NSLog(@"Display very Long: %lld", veryLong);
OUTPUT : Display very Long: 20152015
type : String
NSString *aString = @"A string";
NSLog(@"Display string: %@", aString);
OUTPUT : Display String: a String
type : Float
float aFloat = 5.34245;
NSLog(@"Display Float: %F", aFloat);
OUTPUT : isplay Float: 5.342450
type : Integer
int aInteger = 3;
NSLog(@"Display Integer: %i", aInteger);
OUTPUT : Display Integer: 3
NSLog(@"\nDisplay String: %@ \n\n Display Float: %f \n\n Display Integer: %i", aString, aFloat, aInteger);
OUTPUT : String: a String
Display Float: 5.342450
Display Integer: 3
http://luterr.blogspot.sg/2015/04/example-code-nslog-console-commands-to.html
NSLog(@"%@", digit);
what is shown in console?
NSLog([digit]); // [] are the messages in Objective-C, just like methods or functions in other programming languages
Since you just need to print the value of 'digit'
Either you can call -
NSLog(digit); // A warning would occur - Format string is not a string literal (potentially insecure)
OR
NSLog(@"%@",digit]); // But if you use %@ to reference the object, the warning will go away.
Both the methods will work but the second one is the right way of logging to console.
精彩评论