2016-10-18 40 views
2

我知道這個問題被問了幾次,但我真的不明白這一點。Swift 3中的UnsafePointer

我想從藍牙設備(miband)中提取一個值。 在SWIFT 2,它的工作是這樣的:

但在迅疾3,它拋出一個錯誤:

Cannot invoke initializer for type 'UnsafePointer<Int>' with an argument list of type '(UnsafeRawPointer)' 

而且我不知道如何遷移到SWIFT 3

回答

2

你可以使用withUnsafeBytespointee

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) { 
    if characteristic.uuid.uuidString == "FF06" { 
     let value = characteristic.value!.withUnsafeBytes { (pointer: UnsafePointer<Int>) -> Int in 
      return pointer.pointee 
     } 
     print("Steps: \(value)") 
    } 
} 

我如果UnsafePointer指向Pointee的數組,則可以使用下標運算符,例如, pointer[0],pointer[1]等,而不是pointer.pointee。請參閱SE-0107

+0

完美地工作。謝謝。 – Patricks

相關問題