I have this code right here that worked in preexisting swift.
func toByteArray<T>(_ value: T) -> [UInt8] {
var val = value
//let sock = socket(PF_INET, SOCK_STREAM, 0) // added in 11/16
return withUnsafePointer(to: &val) {
Array(UnsafeBufferPointer(start: UnsafePointer<UInt8>($0), count: MemoryLayout<T>.size))
}
}
This does not work, as it prints an error message that says init is unavailable. Use withMemoryRebound(to:capacity:_). Ok, so I have researched through google and found some options that should work, where now my updated code is
func toByteArray<T>(_ value: T) -> [UInt8] {
var val = value
return withUnsafePointer(to: &val) {
//another swift 3 change with UnsafePointer<UInt8>
Array(UnsafeBufferPointer(start: ($0).withMemoryRebound(to:UInt8.self, capacity: 1){ SCNetworkReachabilityCreateWithAddress(nil, $0)}, count: MemoryLayout<T>.size))
}
}
Which should work, however it says use of unresolved identifier SCNetworkReachabilityCreateWithAddress, which I don't understand and can't find a solution for it. Any thoughts on what the issue is? I feel it is something that is with the updated Swift and Xcode.