Swift 3如何解析数据

问题描述:

我的Swift 3代码有一个基金会Data字节集合(从SQlite BLOB读取)。数据的内部内容具有以下结构的多个块:Swift 3如何解析数据

{ 
    UINT32 count; // number of points in this trkseg 
    UINT32 colour; // RGB colour of the line drawn for this trkseq 
    Double lat; // latitude of 1st point 
    Double long; // longitude of 1st point 
    Coord point[count-1] // array of points (2nd to last points) 
} 

typedef struct { 
    Float lat // difference in latitude of this point from the lat of the 1st point 
    Float long // difference in longitude of this point from the lat of the 1st point 
} Coord; 

这很容易在C和Java中解析。不幸的是,我不能找出用Swift 3解析这个问题的最佳方法。我不是问如何解析这个确切的数据布局,而仅仅是为了解析Swift 3中的这些原始数据的建议和最佳实践。从网络搜索和Apple文档我很遗憾!

***答案 - 感谢Martin R让我走上正轨。我在这里添加一些代码来展示我如何解决这个问题,以防止它帮助其他人。正如马丁所说,有很多方法可以解决这个问题。我的解决方案可以确保无论主机字节顺序如何,blob数据都将正确解析,而网络endian顺序(大端)将始终正确解析。

/// Parse the SQLite data blob to add GPS track segments 
/// 
/// - Parameter data: GPS track information 
private func addTracks(_ data: Data) { 

    // The data holds compressed GPX data. It has multiple track segments. 
    // It has a block of binary data per track segment with this structure: 
    //  { 
    //   UINT32 count; // number of points in this trkseg 
    //   UINT32 colour; // RGB colour of the line drawn for this trkseq 
    //   Double lat; // latitude of 1st point 
    //   Double long; // longitude of 1st point 
    //   Coord point[count-1] // array of points (2nd to last points) 
    // } 
    // 
    // typedef struct { 
    //   Float lat // difference in latitude of this point from the lat of the 1st point 
    //   Float long // difference in longitude of this point from the lat of the 1st point 
    //  } Coord; 

    var dataCount = data.count // number of data bytes 

    var pointCount = 0  // counts coordinates per trkseg 
    var colour:UInt = 0 
    var lat:Double = 0.0 
    var long:Double = 0.0 
    var bigEndian = true 
    var i = 0 

    // From http://codereview.stackexchange.com/questions/114730/type-to-byte-array-conversion-in-swift 
    if (NSHostByteOrder() == NS_LittleEndian) { 
     bigEndian = false 
    } 

    while (dataCount >= 40) { 
     pointCount = Int(self.uint32Value(data: data.subdata(in: i..<i+4), isBigEndian: bigEndian)) 
     i = i+4 

     if (pointCount < 2 || ((pointCount-1)*8 + 24 > dataCount)) { 
      print("ERROR, pointCount=\(pointCount)") 
      break 
     } 
     colour = UInt(self.uint32Value(data: data.subdata(in: i..<i+4), isBigEndian: bigEndian)) 
     i = i+4 
     let firstLat = self.doubleValue(data: data.subdata(in: i..<i+8), isBigEndian: bigEndian) 
     i = i+8 
     let firstLong = self.doubleValue(data: data.subdata(in: i..<i+8), isBigEndian: bigEndian) 
     i = i+8 
     print("pointCount=\(pointCount) colour=\(colour) firstLat=\(firstLat) firstLong=\(firstLong)") 

     for _ in 1..<pointCount { 
      lat = firstLat - Double(self.floatValue(data: data.subdata(in: i..<i+4), isBigEndian: bigEndian)) 
      i = i+4 
      long = firstLong - Double(self.floatValue(data: data.subdata(in: i..<i+4), isBigEndian: bigEndian)) 
      i = i+4 
      print("lat=\(lat) long=\(long)") 
     } 
     dataCount = dataCount - 24 - (pointCount-1)*8; 
    } 
} 

private func floatValue(data: Data, isBigEndian: Bool) -> Float { 
    if (isBigEndian) { 
     return Float(bitPattern: UInt32(littleEndian: data.withUnsafeBytes { $0.pointee })) 
    } 
    else { 
     return Float(bitPattern: UInt32(bigEndian: data.withUnsafeBytes { $0.pointee })) 
    } 
} 

private func doubleValue(data: Data, isBigEndian: Bool) -> Double { 

    if (isBigEndian) { 
     return Double(bitPattern: UInt64(littleEndian: data.withUnsafeBytes { $0.pointee })) 
    } 
    else { 
     return Double(bitPattern: UInt64(bigEndian: data.withUnsafeBytes { $0.pointee })) 
    } 
} 

private func uint32Value(data: Data, isBigEndian: Bool) -> UInt32 { 

    if (isBigEndian) { 
     return data.withUnsafeBytes{ $0.pointee } 
    } 
    else { 
     let temp: UInt32 = data.withUnsafeBytes{ $0.pointee } 
     return temp.bigEndian 
    } 
} 

一种可能的方法是使用

public func withUnsafeBytes<ResultType, ContentType>(_ body: (UnsafePointer<ContentType>) throws -> ResultType) rethrows -> ResultType 

方法来访问和取消引用在所述数据中的字节。 占位类型ContentType可以从上下文推断:

let color: UInt32 = data.subdata(in: 0..<4).withUnsafeBytes { $0.pointee } 
// ... 
let lat: Double = data.subdata(in: 8..<16).withUnsafeBytes { $0.pointee } 
// ... 

随着斯威夫特4,你可以使用下标来提取数据:

let color: UInt32 = data[0..<4].withUnsafeBytes { $0.pointee } 
// ... 
let lat: Double = data[8..<16].withUnsafeBytes { $0.pointee } 
// ... 

如果所有字段都将其类型的正确对齐,那么你可以 使用

public func load<T>(fromByteOffset offset: Int = default, as type: T.Type) -> T 
UnsafeRawPointer

data.withUnsafeBytes { (bytes: UnsafePointer<UInt8>) -> Void in 
    let rawPointer = UnsafeRawPointer(bytes) 
    let color = rawPointer.load(fromByteOffset: 0, as: UInt32.self) 
    // ... 
    let lat = rawPointer.load(fromByteOffset: 8, as: Double.self) 
    // ... 
} 

在一个较低的水平,你可以使用memcpy这又与 任意调整后的数据的工作原理:

var color: UInt32 = 0 
var lat: Double = 0 
data.withUnsafeBytes { (bytes: UnsafePointer<UInt8>) -> Void in 
    memcpy(&color, bytes, 4) 
    // ... 
    memcpy(&lat, bytes + 8, 8) 
    // ... 
} 

我可能会用第一种方法,除非性能是一个 问题,你可以使用第二或第三,这取决于 是否所有字段都保证与其类型对齐。

+0

非常有帮助的回答。数据以网络端序(big endian)存储,因此需要转换为小端。我可以使用“let color = rawPointer.load(fromByteOffset:0,as:UInt32.self).bigEndian”将Uint32值转换为little endian。这种转换不适用于Double。 http://*.com/questions/41161034/how-to-convert-bytes-to-a-float-value-in-swift描述了一种处理转换的方法。这是最简单的吗?我不禁认为这太复杂了,必须有一个更简单的方法。 – pbm

+0

@pbm:你可以先将它读入UInt64(带有endian转换),然后用'Double(bitPattern:...)'将它分配给Double。 - 你如何在C或Java中做到这一点? –