Search code examples
iosobjective-cnslog

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).


Solution

  • 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 e.g. you are using NSMutableData as your buffer, remove any nil bytes from it before trying to decode it into an NSString.