Using NSLog for debugging

前端 未结 6 2208
暗喜
暗喜 2020-12-16 10:02

I have the following code snippet in my Xcode:

NSString *digit [[sender titlelabel] text];
NSLog([digit]);

I tried to build the application

相关标签:
6条回答
  • 2020-12-16 10:39

    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

    0 讨论(0)
  • 2020-12-16 10:40

    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.

    0 讨论(0)
  • 2020-12-16 10:45

    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);
    
    0 讨论(0)
  • 2020-12-16 10:46

    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];

    0 讨论(0)
  • 2020-12-16 10:57

    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.

    0 讨论(0)
  • 2020-12-16 10:59
    NSLog(@"%@", digit);
    

    what is shown in console?

    0 讨论(0)
提交回复
热议问题