How do you convert an iPhone OSStatus code to something useful?

前端 未结 19 1453
执念已碎
执念已碎 2020-12-05 01:45

I am getting more than a little sick of this iPhone SDK and its documentation...

I am calling AudioConverterNew

in the documentation under Returns: it says \

相关标签:
19条回答
  • 2020-12-05 01:50

    No. Not completely.

    Some OSStatus are four-character-codes, so you can use (extracted from iPhone SDK's sample code "CAXException.h")

    static char *FormatError(char *str, OSStatus error)
    {
        // see if it appears to be a 4-char-code
        *(UInt32 *)(str + 1) = CFSwapInt32HostToBig(error);
        if (isprint(str[1]) && isprint(str[2]) && isprint(str[3]) && isprint(str[4])) {
            str[0] = str[5] = '\'';
            str[6] = '\0';
        } else {
            // no, format it as an integer
            sprintf(str, "%d", (int)error);
        }
        return str;
    }
    

    (See iOS/C: Convert "integer" into four character string for some more ways to convert fourcc into string, including Swift)

    NSError's NSOSStatusErrorDomain is able to decode some OS errors. See @tomk's answer.

    If you don't need to decode the number in program for the user, you may use the macerror script to manually find out the meaning, as mentioned in @lros's answer. The list of OSStatus supported can be found from its source code in /System/Library/Perl/Extras/5.18/Mac/Errors.pm.

    There is also an online service http://osstatus.com/ collecting errors from all public frameworks. They are still not really complete e.g. the mapping to -12792 mentioned in the comment is missing. Probably it is a code from a private framework.

    0 讨论(0)
  • 2020-12-05 01:51

    I recently ran across another approach: the macerror command. Print out the OSStatus value as a signed integer. Then in a terminal window (on your Mac, not your iDevice!) type for example macerror -50. It will respond with a brief description. Obviously this is only helpful for you, during development.

    0 讨论(0)
  • 2020-12-05 01:53

    OSStatus err; ... printf("%s", (char*)&err);

    0 讨论(0)
  • 2020-12-05 01:55

    I recently found this really nice website that's worked for every status value I've thrown at it. It's a lot more user friendly than grepping through the framework header files: http://www.osstatus.com/

    0 讨论(0)
  • 2020-12-05 01:55

    For iOS 11.3+, I'm using an extension on OSStatus

    extension OSStatus {
    
        var error: NSError? {
            guard self != errSecSuccess else { return nil }
    
            let message = SecCopyErrorMessageString(self, nil) as String? ?? "Unknown error"
    
            return NSError(domain: NSOSStatusErrorDomain, code: Int(self), userInfo: [
                NSLocalizedDescriptionKey: message])
        }
    }
    

    which you can call like…

    let status = SecItemAdd(attributes as CFDictionary, nil)
    
    if let error = status.error {
        throw error
    }    
    // etc
    

    Having written this I noticed this is very close to @RomanMykitchak's earlier answer (so please give him the upvote) - but I'll leave it here as the extension might prove useful to someone.

    0 讨论(0)
  • 2020-12-05 02:01

    Failing a description string, it's convenient to convert OSStatus values to strings that look like their four-character definitions. At least then you can grep headers in hopes of finding a comment about what the status means.

    // declaration:  extern CFStringRef CreateTypeStringWithOSType(OSType inType);
    
    OSStatus result = ...;
    
    if (result != noErr) {
        NSString *statusString = (NSString *)CreateTypeStringWithOSType(result);
        NSLog(@"Error while $VERBing: %@", statusString);
        [statusString release]; // because "Create..."
        statusString = nil;
    }
    
    0 讨论(0)
提交回复
热议问题