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

前端 未结 19 1617
执念已碎
执念已碎 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: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.

提交回复
热议问题