Created
April 11, 2017 05:02
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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