【问题标题】:NSLog stops printing after third variableNSLog 在第三个变量后停止打印
【发布时间】:2014-05-14 09:14:14
【问题描述】:

我目前正在开发一个低功耗蓝牙应用程序,我想使用 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: 所以我做了一些测试,结果是 Modell、SerialNumber 和 Batterie 在第一次执行我的 NSLog 语句时都为空。当第一个 String Modell 最终成为一个真实值(“H7”)时,这在最初的几次迭代中保持不变。这是第一次 NSLog 只打印 Modell 而忽略其他的(因为它们仍然是 null 并且将在一次迭代后成为一个值)。

【问题讨论】:

  • 尝试清理并构建一次
  • 它不打印到日志消息或变量?
  • 你应该单步调试调试器中的代码,看看到底发生了什么。或尝试使用my logging "framework",它会告诉您日志消息的文件名和行号,而不是多余的日期和程序名称。
  • NSLog(@"Modell:%@", self.modelNumber); NSLog(@"SerialNumber:%@", self.serialNumber); NSLog(@"电池:%@",self.batteryLevel);使用它,现在检查你被击中的位置,同时删除所有断点并进行测试。
  • 你是在看 Xcode 的控制台输出,还是在设备的控制台输出?

标签: ios objective-c nslog


【解决方案1】:

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 通信渠道获得的字符串值不包含零字节。如果例如您正在使用 NSMutableData 作为缓冲区,请在尝试将其解码为 NSString 之前从中删除任何 nil 字节。

【讨论】:

  • 这个理论只有一个问题:OP 的 NSLog 行包含 @"Modell:%@ SerialNumber:%@ Batterie:%@" 并且输出首先跳过 Modell。然后它打印序列号,然后跳过电池。你怎么解释这个?
  • @Michael 好点——如果在此处输入 SO 问题不是错误,那么该日志消息可能来自其他地方。我建议 OP 使用调试器来验证这一点。
  • 所以我做了一些测试,结果是 Modell、SerialNumber 和 Batterie 在第一次执行我的 NSLog 语句时都为空。当第一个 String Modell 最终成为一个真实值(“H7”)时,这在最初的几次迭代中保持不变。这是第一次 NSLog 只打印 Modell 而忽略其他的(因为它们仍然是 null 并且将在一次迭代后成为一个值)。所以我想你是对的,虽然我需要弄清楚如何解决这个问题......
  • @Evils:您是否使用调试器单步执行了 NSLog 语句?
  • @Michael 是的,我使用调试器查看了这些语句。日志消息绝对不是来自其他地方。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    为什么NSLog 语句会跳过“Modell”然后打印“SerialNumber”?我认为日志消息的来源与您想象的不同。

    要解决这个问题,您应该在两个NSLog 行上放置一个断点并单步执行它们。然后,会发生以下两种情况之一:要么断点不会停止程序,因为日志输出来自不同的地方。或者,断点停止,当你单步执行它时,它就可以工作了。

    举个例子

    // 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);
    

    这给出了以下输出

    // 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
    

    当您查看“第 2 部分源代码”并将其与“第 1 部分输出”进行比较时,它似乎不起作用。这可能会更糟,当“第 2 部分源代码”可能从未执行时。

    NSLog 的根本问题在于您永远无法确定消息来自何处。仅仅因为您在源代码中看到NSLog(@"hello");,并且日志显示hello,并不意味着您看到的NSLog 行就是产生日志输出的行。 NSLog(@"hello") 的源中可能有多个位置,并且只有一个位置产生了输出。

    为了将来解决这个问题,您可以使用一个日志框架,它会告诉您正在创建的日志条目的文件名和行号。 (例如MPLog(开源,由我制作))

    【讨论】:

      猜你喜欢
      • 2020-06-24
      • 1970-01-01
      • 2020-11-30
      • 2022-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多