我目前正在开发一个低功耗蓝牙应用程序,我想在其中使用 NSLog 来打印我收集到的信息。
这是我在 didUpdateValueForCharacteristic 方法末尾使用的代码,此时所有属性都已更新(调试器确认):
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);
这些属性实际上只是标准的:
@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;
NSLog 输出如下:
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
我无法控制为什么它不打印 SerialNumber:self.serialNumber 和 Batterie:self.batteryLevel。我以前在一行中有两个 NSLog 命令,但同样的事情发生在这里。我在打印第三个属性后被剪掉了(所以连 SerialNumber 都不见了)。
有什么想法吗?
编辑 1: 所以我做了一些测试,结果是在第一次执行我的 NSLog 语句时,Modell、SerialNumber 和 Batterie 都为空。当第一个字符串模型最终成为一个真正的值(“H7”)时,这会保留前几次迭代。这是 NSLog 第一次只打印 Modell 而忽略其他的(因为它们仍然是 null 并且将在一次迭代后成为一个值)。
最佳答案
当 NSString
对象包含零字节 (\0
) 时,我看到过这种行为。这是一个说明问题的测试程序:
#import <Foundation/Foundation.h>
int main() {
NSString *a = @"aa\0a";
NSString *b = @"bbb";
NSLog(@"a: %@, b: %@, end.", a, b);
}
运行上面的程序:
$ clang -framework Foundation test.m -o test && ./test
2014-05-14 13:11:52.912 test[17814:507] a: aa
因此请确保您从 BT 通信 channel 获取的字符串值不包含零字节。如果例如如果您使用 NSMutableData 作为缓冲区,请在尝试将其解码为 NSString 之前从中删除所有 nil 字节。
关于ios - NSLog 在第三个变量后停止打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23650329/