二进制运算符'=='不能应用于'Int8'和'Int'类型的操作数

时间:2017-03-29 23:04:58

标签: swift swift3

因为我将代码转换为Swift 3我得到了这个错误:

Binary Operator '==' can not be applied to operands of type 'Int8' and 'Int'

在Swift 2中,它使用了这个:

    var flags : UInt8
    var count : Int = 1
    var zw = [UInt8](repeating: 0, count: 2)

    let countTotal = data.count / MemoryLayout<UInt8>.size

    var bytes = [UInt8](repeating: 0, count: countTotal)

    data.copyBytes(to: &bytes, count: countTotal*MemoryLayout<UInt8>.size)

    flags = bytes[0]
    if([0x01 & flags] == [0 & 0x01]) // **<--ERROR**
    {
        zw[0] = bytes[count]
        zw[1] = bytes[count + 1
        let bpsys = UnsafePointer(zw).withMemoryRebound(to: UInt16.self, capacity: 1)
        {
            $0.pointee
        }

1 个答案:

答案 0 :(得分:2)

您使用了错误的括号([而不是()。但看起来Swift 3以一种微妙的方式改变了类型推理系统,从而破坏了你的程序。试试这个:

if (0x01 & flags) == (0 & 0x01) {

}

最后一个问题是为什么? 0 & 0x01总是返回0.你也可以写:

if 0x01 & flag == 0 {

}