2017-02-15 35 views
0

負荷iPhone5的(10.2)模擬器此功能:致命錯誤:從錯位原始指針

static func fromByteArray<T>(_ value: [UInt8], _: T.Type) -> T { 
     return value.withUnsafeBytes { 
      $0.baseAddress!.load(as: T.self) 
     } 
    } 

崩潰在$0.baseAddress!.load(as: T.self)與此錯誤:

fatal error: load from misaligned raw pointer

是否有人知道解決?

我使用這個代碼:

https://stackoverflow.com/a/26954091/1979882

編輯 它適用於iPhone5s但不是iPhone5的

回答

2

事實上,它像你描述的崩潰。解決的辦法是使用此函數字節數組轉換成所需類型:

func fromByteArray<T>(_ value: [UInt8], _: T.Type) -> T { 
     return value.withUnsafeBufferPointer { 
      $0.baseAddress!.withMemoryRebound(to: T.self, capacity: 1) { 
       $0.pointee 
      } 
     } 
    } 

下面是從測試我的結果(紅色崩潰iPhone 5的模擬器未知的原因): enter image description here