Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Because of the way Swift initializers work the way Swift initializers work, having this initializer saves us from having to assign every property in every single initializer. If we know any of our other failable initializers have entered a failure state, we just call this and we're done.

Because of the way Swift initializers work, having this initializer saves us from having to assign every property in every single initializer. If we know any of our other failable initializers have entered a failure state, we just call this and we're done.

Because of the way Swift initializers work, having this initializer saves us from having to assign every property in every single initializer. If we know any of our other failable initializers have entered a failure state, we just call this and we're done.

added `.reverse()` call to the arrays, based on data sanity checks for endianness
Source Link
nhgrif
  • 25.4k
  • 3
  • 64
  • 129
import Foundation
extension UInt16 {
 init?(bytes: [UInt8]) {
 if bytes.count != 2 {
 return nil
 }
 
 var value: UInt16 = 0
 for byte in bytes.reverse() {
 value = value << 8
 value = value | UInt16(byte)
 }
 self = value
 }
}
extension UInt32 {
 init?(bytes: [UInt8]) {
 if bytes.count != 4 {
 return nil
 }
 
 var value: UInt32 = 0
 for byte in bytes.reverse() {
 value = value << 8
 value = value | UInt32(byte)
 }
 self = value
 }
}
let bytes: [UInt8] = [0x4e, 0x5b, 0xa2, 0x56, 0x12, 0x19, 0x00, 0x00, 0x22, 0x00, 0x12,0x19, 0x07, 0x00]
let data = NSData(bytes: bytes, length: bytes.count)
class Information {
 let startTime: UInt32
 let duration: UInt32
 let temperature: UInt16
 let estimatedDistance: UInt16
 let knockCount: UInt8
 let reserved: UInt8
 
 private init?(fail: Bool = true) {
 self.startTime = 0
 self.duration = 0
 self.temperature = 0
 self.estimatedDistance = 0
 self.knockCount = 0
 self.reserved = 0
 if fail {
 return nil
 }
 }
 
 init(startTime: UInt32, duration: UInt32, temperature: UInt16, estimatedDistance: UInt16, knockCount: UInt8, reserved: UInt8) {
 self.startTime = startTime
 self.duration = duration
 self.temperature = temperature
 self.estimatedDistance = estimatedDistance
 self.knockCount = knockCount
 self.reserved = reserved
 }
 
 convenience init?(startTimeBytes: [UInt8], durationBytes: [UInt8], temperatureBytes: [UInt8], estimatedDistanceBytes: [UInt8], knockCount: UInt8, reserved: UInt8) {
 if let
 startTime = UInt32(bytes: startTimeBytes),
 duration = UInt32(bytes: durationBytes),
 temperature = UInt16(bytes: temperatureBytes),
 estimatedDistance = UInt16(bytes: estimatedDistanceBytes) {
 self.init(startTime: startTime, duration: duration, temperature: temperature, estimatedDistance: estimatedDistance, knockCount: knockCount, reserved: reserved)
 } else {
 self.init(fail: true)
 }
 }
 
 convenience init?(data: NSData) {
 if data.length == 14 {
 let allBytes = Array(UnsafeBufferPointer(start: UnsafePointer<UInt8>(data.bytes), count: data.length))
 self.init(startTimeBytes: Array(allBytes[0..<4]), durationBytes: Array(allBytes[4..<8]), temperatureBytes: Array(allBytes[8..<10]), estimatedDistanceBytes: Array(allBytes[10..<12]), knockCount: allBytes[12], reserved: allBytes[13])
 } else {
 self.init(fail: true)
 }
 }
}
func stringFromTimeInterval(interval:NSTimeInterval) -> String {
 let ti = NSInteger(interval)
 
 let ms = Int((interval % 1) * 1000)
 
 let seconds = ti % 60
 let minutes = (ti / 60) % 60
 let hours = (ti / 3600)
 
 return NSString(format: "%0.2d:%0.2d:%0.2d.%0.3d",hours,minutes,seconds,ms) as String
}
let i = Information(data: data)!
printlet timestamp = NSDate(timeIntervalSince1970: NSTimeInterval(i?.temperaturestartTime))
let duration = stringFromTimeInterval(NSTimeInterval(i.duration))
let temperature = i.temperature
let estimatedDistance = i.estimatedDistance
let knockCount = i.knockCount
let reserved = i.reserved

And this is the result I get for the given data:

enter image description here

import Foundation
extension UInt16 {
 init?(bytes: [UInt8]) {
 if bytes.count != 2 {
 return nil
 }
 
 var value: UInt16 = 0
 for byte in bytes {
 value = value << 8
 value = value | UInt16(byte)
 }
 self = value
 }
}
extension UInt32 {
 init?(bytes: [UInt8]) {
 if bytes.count != 4 {
 return nil
 }
 
 var value: UInt32 = 0
 for byte in bytes {
 value = value << 8
 value = value | UInt32(byte)
 }
 self = value
 }
}
let bytes: [UInt8] = [0x4e, 0x5b, 0xa2, 0x56, 0x12, 0x19, 0x00, 0x00, 0x22, 0x00, 0x12,0x19, 0x07, 0x00]
let data = NSData(bytes: bytes, length: bytes.count)
class Information {
 let startTime: UInt32
 let duration: UInt32
 let temperature: UInt16
 let estimatedDistance: UInt16
 let knockCount: UInt8
 let reserved: UInt8
 
 private init?(fail: Bool = true) {
 self.startTime = 0
 self.duration = 0
 self.temperature = 0
 self.estimatedDistance = 0
 self.knockCount = 0
 self.reserved = 0
 if fail {
 return nil
 }
 }
 
 init(startTime: UInt32, duration: UInt32, temperature: UInt16, estimatedDistance: UInt16, knockCount: UInt8, reserved: UInt8) {
 self.startTime = startTime
 self.duration = duration
 self.temperature = temperature
 self.estimatedDistance = estimatedDistance
 self.knockCount = knockCount
 self.reserved = reserved
 }
 
 convenience init?(startTimeBytes: [UInt8], durationBytes: [UInt8], temperatureBytes: [UInt8], estimatedDistanceBytes: [UInt8], knockCount: UInt8, reserved: UInt8) {
 if let
 startTime = UInt32(bytes: startTimeBytes),
 duration = UInt32(bytes: durationBytes),
 temperature = UInt16(bytes: temperatureBytes),
 estimatedDistance = UInt16(bytes: estimatedDistanceBytes) {
 self.init(startTime: startTime, duration: duration, temperature: temperature, estimatedDistance: estimatedDistance, knockCount: knockCount, reserved: reserved)
 } else {
 self.init(fail: true)
 }
 }
 
 convenience init?(data: NSData) {
 if data.length == 14 {
 let allBytes = Array(UnsafeBufferPointer(start: UnsafePointer<UInt8>(data.bytes), count: data.length))
 self.init(startTimeBytes: Array(allBytes[0..<4]), durationBytes: Array(allBytes[4..<8]), temperatureBytes: Array(allBytes[8..<10]), estimatedDistanceBytes: Array(allBytes[10..<12]), knockCount: allBytes[12], reserved: allBytes[13])
 } else {
 self.init(fail: true)
 }
 }
}
let i = Information(data: data)
print(i?.temperature)
import Foundation
extension UInt16 {
 init?(bytes: [UInt8]) {
 if bytes.count != 2 {
 return nil
 }
 
 var value: UInt16 = 0
 for byte in bytes.reverse() {
 value = value << 8
 value = value | UInt16(byte)
 }
 self = value
 }
}
extension UInt32 {
 init?(bytes: [UInt8]) {
 if bytes.count != 4 {
 return nil
 }
 
 var value: UInt32 = 0
 for byte in bytes.reverse() {
 value = value << 8
 value = value | UInt32(byte)
 }
 self = value
 }
}
let bytes: [UInt8] = [0x4e, 0x5b, 0xa2, 0x56, 0x12, 0x19, 0x00, 0x00, 0x22, 0x00, 0x12,0x19, 0x07, 0x00]
let data = NSData(bytes: bytes, length: bytes.count)
class Information {
 let startTime: UInt32
 let duration: UInt32
 let temperature: UInt16
 let estimatedDistance: UInt16
 let knockCount: UInt8
 let reserved: UInt8
 
 private init?(fail: Bool = true) {
 self.startTime = 0
 self.duration = 0
 self.temperature = 0
 self.estimatedDistance = 0
 self.knockCount = 0
 self.reserved = 0
 if fail {
 return nil
 }
 }
 
 init(startTime: UInt32, duration: UInt32, temperature: UInt16, estimatedDistance: UInt16, knockCount: UInt8, reserved: UInt8) {
 self.startTime = startTime
 self.duration = duration
 self.temperature = temperature
 self.estimatedDistance = estimatedDistance
 self.knockCount = knockCount
 self.reserved = reserved
 }
 
 convenience init?(startTimeBytes: [UInt8], durationBytes: [UInt8], temperatureBytes: [UInt8], estimatedDistanceBytes: [UInt8], knockCount: UInt8, reserved: UInt8) {
 if let
 startTime = UInt32(bytes: startTimeBytes),
 duration = UInt32(bytes: durationBytes),
 temperature = UInt16(bytes: temperatureBytes),
 estimatedDistance = UInt16(bytes: estimatedDistanceBytes) {
 self.init(startTime: startTime, duration: duration, temperature: temperature, estimatedDistance: estimatedDistance, knockCount: knockCount, reserved: reserved)
 } else {
 self.init(fail: true)
 }
 }
 
 convenience init?(data: NSData) {
 if data.length == 14 {
 let allBytes = Array(UnsafeBufferPointer(start: UnsafePointer<UInt8>(data.bytes), count: data.length))
 self.init(startTimeBytes: Array(allBytes[0..<4]), durationBytes: Array(allBytes[4..<8]), temperatureBytes: Array(allBytes[8..<10]), estimatedDistanceBytes: Array(allBytes[10..<12]), knockCount: allBytes[12], reserved: allBytes[13])
 } else {
 self.init(fail: true)
 }
 }
}
func stringFromTimeInterval(interval:NSTimeInterval) -> String {
 let ti = NSInteger(interval)
 
 let ms = Int((interval % 1) * 1000)
 
 let seconds = ti % 60
 let minutes = (ti / 60) % 60
 let hours = (ti / 3600)
 
 return NSString(format: "%0.2d:%0.2d:%0.2d.%0.3d",hours,minutes,seconds,ms) as String
}
let i = Information(data: data)!
let timestamp = NSDate(timeIntervalSince1970: NSTimeInterval(i.startTime))
let duration = stringFromTimeInterval(NSTimeInterval(i.duration))
let temperature = i.temperature
let estimatedDistance = i.estimatedDistance
let knockCount = i.knockCount
let reserved = i.reserved

And this is the result I get for the given data:

enter image description here

added `.reverse()` call to the arrays, based on data sanity checks for endianness
Source Link
nhgrif
  • 25.4k
  • 3
  • 64
  • 129
extension UInt16 {
 init?(bytes: [UInt8]) {
 if bytes.count != 2 {
 return nil
 }
 
 var value: UInt16 = 0
 for byte in bytes.reverse() {
 value = value << 8
 value = value | UInt16(byte)
 }
 self = value
 }
}
extension UInt32 {
 init?(bytes: [UInt8]) {
 if bytes.count != 4 {
 return nil
 }
 
 var value: UInt32 = 0
 for byte in bytes.reverse() {
 value = value << 8
 value = value | UInt32(byte)
 }
 self = value
 }
}
extension UInt16 {
 init?(bytes: [UInt8]) {
 if bytes.count != 2 {
 return nil
 }
 
 var value: UInt16 = 0
 for byte in bytes {
 value = value << 8
 value = value | UInt16(byte)
 }
 self = value
 }
}
extension UInt32 {
 init?(bytes: [UInt8]) {
 if bytes.count != 4 {
 return nil
 }
 
 var value: UInt32 = 0
 for byte in bytes {
 value = value << 8
 value = value | UInt32(byte)
 }
 self = value
 }
}
extension UInt16 {
 init?(bytes: [UInt8]) {
 if bytes.count != 2 {
 return nil
 }
 
 var value: UInt16 = 0
 for byte in bytes.reverse() {
 value = value << 8
 value = value | UInt16(byte)
 }
 self = value
 }
}
extension UInt32 {
 init?(bytes: [UInt8]) {
 if bytes.count != 4 {
 return nil
 }
 
 var value: UInt32 = 0
 for byte in bytes.reverse() {
 value = value << 8
 value = value | UInt32(byte)
 }
 self = value
 }
}
Source Link
nhgrif
  • 25.4k
  • 3
  • 64
  • 129
Loading
default

AltStyle によって変換されたページ (->オリジナル) /