简体   繁体   中英

NSLog stops printing after third variable

I'm currently working on a Bluetooth LowEnergy application where I would like to use NSLog to print my gathered Information.

This is the code I'm using at the end of my didUpdateValueForCharacteristic Method when all properties are already update (debugger confirms that):

NSLog(@"These are the information I found! %huBPM Position:%@ Hersteller:%@ ", self.heartRate, self.bodySensorLocation, self.manufacturerName);
NSLog(@"Modell:%@ SerialNumber:%@ Batterie:%@", self.modelNumber, self.serialNumber, self.batteryLevel);

The properties are really just standard:

@property (nonatomic, strong) NSString *manufacturerName;
@property (nonatomic, strong) NSString *modelNumber;
@property (nonatomic, strong) NSString *serialNumber;
@property (nonatomic, strong) NSString *batteryLevel;
@property (nonatomic, strong) NSString *bodySensorLocation;
@property (assign) uint16_t heartRate;

What NSLog outputs is the following:

2014-05-14 11:04:16.888 PulseSense[24493:60b] These are the information I found! 77BPM Position:Chest Hersteller:Polar Electro Oy
2014-05-14 11:04:16.889 PulseSense[24493:60b] SerialNumber:1312082746

I can't help myself why it wont print SerialNumber:self.serialNumber and Batterie:self.batteryLevel. I had both NSLog commands in one line before, but the same thing happened here. I got cut after printing the third property (so even SerialNumber was missing).

Any ideas?

Edit 1: So I did some test and what happens is that Modell, SerialNumber and Batterie are all null with the first execution of my NSLog Statements. This stays for the first few iterations when finally the first String Modell becomes a real Value ("H7"). This is then the first time when NSLog only prints Modell and leaves the others out (as they are still null and will become a value one iteration later).

I have seen this kind of behavior when NSString objects contain nil bytes ( \\0 ). Here's a test program that illustrates the issue:

#import <Foundation/Foundation.h>

int main() {
    NSString *a = @"aa\0a";
    NSString *b = @"bbb";
    NSLog(@"a: %@, b: %@, end.", a, b);
}

Running the above program:

$ clang -framework Foundation test.m -o test && ./test
2014-05-14 13:11:52.912 test[17814:507] a: aa

So ensure that the string values you are getting from your BT communications channel do not contain nil bytes. If eg you are using NSMutableData as your buffer, remove any nil bytes from it before trying to decode it into an NSString .

确保第三项包含要打印的有效值,而不仅仅是该对象的nil

Why is the NSLog statement skipping the "Modell" and then printing the "SerialNumber"? I think the log messages come from a different place than you think.

To solve this problem, you should put a breakpoint on the two NSLog lines and single-step through them. Then, one of two things will happen: Either, the breakpoint will not stop the program, because the log-output comes from a different place. Or, the breakpoint stops, and when you single-step through it, it just works.

Let me give you an example

// part 1 source
NSLog(@"These are the information I found! %huBPM Position:%@ Hersteller:%@ ", self.heartRate, self.bodySensorLocation, self.manufacturerName);
NSLog(@"SerialNumber:%@", self.serialNumber);

...

// part 2 source
NSLog(@"These are the information I found! %huBPM Position:%@ Hersteller:%@ ", self.heartRate, self.bodySensorLocation, self.manufacturerName);
NSLog(@"Modell:%@ SerialNumber:%@ Batterie:%@", self.modelNumber, self.serialNumber, self.batteryLevel);

This gives the following output

// part 1 output
2014-05-14 11:04:16.888 PulseSense[24493:60b] These are the information I found! 77BPM Position:Chest Hersteller:Polar Electro Oy
2014-05-14 11:04:16.889 PulseSense[24493:60b] SerialNumber:1312082746

...

// part 2 output
2014-05-14 11:04:16.888 PulseSense[24493:60b] These are the information I found! 77BPM Position:Chest Hersteller:Polar Electro Oy
2014-05-14 11:04:16.889 PulseSense[24493:60b] Modell:Audi SerialNumber:1312082746 Batterie:LiIon

When you are looking at "part 2 source" and compare it to "part 1 output", it seems like it doesn't work. This can be worse, when maybe the "part 2 source" is never executed.

The underlying problem with NSLog is just that you can never be sure where the message came from. Just because you see NSLog(@"hello"); in the source, and the log says hello , doesn't mean that the NSLog-line you are seeing was the one that produced the log output. There can be multiple places in the source that NSLog(@"hello") and only one of them produced the output.

To fix this problem for the future, you may use a logging framework that tells you the filename and linenumber of the log entries you are making. (eg with MPLog (opensource, made by me) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM