Swift协议扩展:致命错误:不同大小的类型之间可能出现unsafeBitCast

时间:2016-02-09 01:46:42

标签: swift2 protocol-extension

我已经定义了一个带有协议扩展的协议,以简化NSError的使用。

protocol NSErrorConvertible: RawRepresentable {
    var domain: String { get }
    var localizedDescription: String { get }
}

extension NSErrorConvertible where RawValue == Int {
    func generateError(parameters parameters: [String] = []) -> NSError {
        let error = self.generateError(format: self.localizedDescription, parameters: parameters)
        return error
    }

    func generateError(format format: String, parameters: [String] = []) -> NSError {
        let description = String(format: format, arguments: parameters) // <-- BREAK
        let error = NSError(domain: self.domain, code: self.rawValue, localizedDescription: description)
        return error
    }
}

extension NSError {
    convenience init(domain: String, code: Int, localizedDescription: String) {
        let userInfo = [NSLocalizedDescriptionKey : localizedDescription]
        self.init(domain: domain, code: code, userInfo: userInfo)
    }
}

以下是协议的使用方法:

enum DefaultEngineErrors: Int, NSErrorConvertible {
    case ImagesNotSupported
    case FooDoesNotHaveABar
    case NilBar

    var domain: String { return "DefaultEngineErrors" }

    var localizedDescription: String {
        switch self {
        case .ImagesNotSupported: return "%s: Images are not supported."
        case .FooDoesNotHaveABar: return "%s: Foo does not have a bar."
        case .NilBar:             return "%s: The bar is nil."
        }
    }
}

以下是调用协议扩展方法的方法:

let error = DefaultEngineErrors.FooDoesNotHaveABar.generateError(parameters: ["\(foo.id)"])

运行此代码时,调试器会在第二个generateError扩展函数中构建描述的行中断,并在调试器控制台中显示以下消息:

致命错误:不同尺寸类型之间的不安全比特

请注意,如果我用String(format:)替换format的来电,一切正常。

问:我在这里做了什么错了?

除此之外,我无法检查协议扩展中的参数。我尝试时显示以下消息:

(lldb) po format
error: <EXPR>:1:1: error: non-nominal type '$__lldb_context' (aka 'Self') cannot be extended
extension $__lldb_context {                            
^         ~~~~~~~~~~~~~~~
<EXPR>:15:5: error: value of type 'DefaultEngineErrors' has no member '$__lldb_wrapped_expr_54'
    $__lldb_injected_self.$__lldb_wrapped_expr_54(     
    ^~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~

1 个答案:

答案 0 :(得分:1)

Swift string Array is not compatible with CVarArgType... or [CVarArgType]

尝试使用。

extension NSErrorConvertible where RawValue == Int {
    func generateError(format format: String) -> NSError {
        let error = NSError(domain: self.domain, code: self.rawValue, localizedDescription: format)
        return error
    }
    func generateError(args : CVarArgType...) -> NSError {
        let returnString = NSString(format: self.localizedDescription, arguments: getVaList(args)) as String
//        let returnString = withVaList(args) {
//            NSString(format: self.localizedDescription, arguments: $0)
//        } as String
        let error = generateError(format: returnString)
        return error
    }
}

enum DefaultEngineErrors: Int, NSErrorConvertible {
    case ImagesNotSupported
    case FooDoesNotHaveABar
    case FooWithMultiPar
    case NilBar

    var domain: String { return "DefaultEngineErrors" }

    var localizedDescription: String {
        switch self {
        case .ImagesNotSupported: return "%@: Images are not supported."
        case .FooDoesNotHaveABar: return "%@: Foo does not have a bar."
        case .FooWithMultiPar: return "%@:%@ Foo does not have a bar."
        case .NilBar:             return "%@: The bar is nil."
        }
    }
}

print(DefaultEngineErrors.FooDoesNotHaveABar.generateError("test"))
print(DefaultEngineErrors.FooWithMultiPar.generateError("test1","test2"))

检查this