从 NSData 获取 Int

Get Int from NSData

我正在使用一个库,该库能够从 BTLE 设备中提取数据并将其放置在 NSData 中。但是我需要将单独的数据行提取到 int 中。我该怎么做?

代码如下:

- (void)realDataCallBackWithData:(NSData *)realData{
      if (realData == nil) {
              DLog(@"error");
              return;
      }
      VTRealObject *rObj = [VTO2Parser parseO2RealObjectWithData:realData];
      self.descLab.text = [rObj description];
}

- (UILabel *)descLab{
      if (!_descLab) {
              _descLab =  elalloc initWithFrame:[UIScreen mainScreen].bounds];
              _descLab.numberOfLines = 0;
              _descLab.textAlignment = NSTextAlignmentCenter;
              [self.view addSubview:_descLab];
      }
      return _descLab;
}

这里是库 VTO2Parser.h 代码:

#import <Foundation/Foundation.h>
#import "VTO2Info.h"
#import "VTO2Object.h"
#import "VTO2WaveObject.h"
#import "VTRealObject.h"

NS_ASSUME_NONNULL_BEGIN

@interface VTO2Parser : NSObject

// @brief parse O2 information
// @param infoData infoData from peripheral
+ (VTO2Info *)parseO2InfoWithData:(NSData * _Nonnull)infoData;

// @brief parse O2 object
// @param fileData fileData from peripheral
+ (VTO2Object *)parseO2ObjectWithData:(NSData * _Nonnull)fileData;

// @brief parse O2 Wave array .
// @param waveData waveData from   VTO2Object
+ (NSArray <VTO2WaveObject *>*)parseO2WaveObjectArrayWithWaveData:(NSData * 
_Nonnull)waveData;

// @brief parse O2 Real-time data
// @param realData realData from peripheral
+ (VTRealObject *)parseO2RealObjectWithData:(NSData *)realData;

// @brief parse O2 Real PPG data
// @param realPPG real PPG data from peripheral
+ (NSArray <VTRealPPG *>*)parseO2RealPPGWithData:(NSData *)realPPG;

@end

NS_ASSUME_NONNULL_END

这里还有 VTO2Object.h

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface VTRealObject : NSObject

// @brief blood oxygen value
@property (nonatomic, assign) u_char spo2;

// @brief heart rate
@property (nonatomic, assign) u_short hr;

// @brief battery value
@property (nonatomic, assign) u_char battery;

// @brief battery status
@property (nonatomic, assign) u_char batState;

// @brief Perfusion Index value
@property (nonatomic, assign) u_char pi;

// @brief lead status . for BabyO2:       0: Probe off     1: Lead normal       2: Lead off
@property (nonatomic, assign) u_char leadState;

// @brief motion value
@property (nonatomic, assign) u_char vector; 

@end


@interface VTRealPPG : NSObject

// @brief Infrared
@property (nonatomic, assign) int ir;

// @brief RLED
@property (nonatomic, assign) int red;

// @brief motion
@property (nonatomic, assign) u_char motion;

@end


NS_ASSUME_NONNULL_END

应用程序中的最终结果如下所示:

<VTRealObject: 0x28368ae60> -- { batState = 0; battery = 66; hr = 76; leadState = 1; pi =20; spo2 = 98; vector = 0;}

如何从上述数据中获取单独的 Int,以便将其分配给各个标签?

您的 VTRealObject rObj 可能已经包含您想要的所有值,并将它们作为属性或方法 return 值交给您。

如果不是,那么您需要执行您的 VTO2Parser 已经在做的事情:直接查看 realData 并解析它,找到它包含的值并解释它们。为此,您必须了解 realData 格式 。那么任务就很简单了。例如,如果您知道 realData 的前两个/四个字节是代表 batStatelong,那么将它们读作 long 就很容易了。等等。