Skip to content

Instantly share code, notes, and snippets.

@derekli66
Created April 11, 2017 05:02
Show Gist options
  • Save derekli66/f3fd85d2b11ef7d8187974fec2f1f15c to your computer and use it in GitHub Desktop.
Save derekli66/f3fd85d2b11ef7d8187974fec2f1f15c to your computer and use it in GitHub Desktop.
OSStatus error check function in Swift. Print out OSStatus error and display error message as needed.
public func CheckError(_ error: OSStatus, _ operation: String) -> Void
{
if (error == noErr) { return }
let count = 5
let stride = MemoryLayout<OSStatus>.stride
let byteCount = stride * count
var error_ = CFSwapInt32HostToBig(UInt32(error))
var charArray: [CChar] = [CChar](repeating: 0, count: byteCount )
withUnsafeBytes(of: &error_) { (buffer: UnsafeRawBufferPointer) in
for (index, byte) in buffer.enumerated() {
charArray[index + 1] = CChar(byte)
}
}
let v1 = charArray[1], v2 = charArray[2], v3 = charArray[3], v4 = charArray[4]
if (isprint(Int32(v1)) > 0 && isprint(Int32(v2)) > 0 && isprint(Int32(v3)) > 0 && isprint(Int32(v4)) > 0) {
charArray[0] = "\'".utf8CString[0]
charArray[5] = "\'".utf8CString[0]
let errStr = NSString(bytes: &charArray, length: charArray.count, encoding: String.Encoding.ascii.rawValue)
print("Error: \(operation) (\(errStr!))")
}
else {
print("Error: \(error)")
}
exit(1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment