在swift通用函数中移位 [英] Bit shifting in swift generic function

查看:116
本文介绍了在swift通用函数中移位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个需要位移操作的通用函数。我得到了我不明白的行为。

  func testBytes< T:IntegerType>(bytesIn:[UInt8],inout dataOut :T){

let outputSize = sizeof(T)
var temp:T = 0
dataOut = 0
temp = bytesIn [0] as T
temp = temp<< 1

}

如果我这样做,那么最后一行给我xcode中的错误T不能转换为Int。

我可以将最后一行更改为 temp = temp<< (1 as T)



然后这行代码的错误改变为T不能转换为UInt8



在这种情况下,这些错误信息都不适合我。有什么我可以做的,以启用位泛型类型转移?

解决方案

我有一个关于这个话题的博客文章,这些内容会有更详细的介绍,但基本上有三个步骤:
$ b


  1. 使用bitshift运算符和一个来自 UInt8

     协议BitshiftOperationsType {
    func< <(lhs:Self,rhs:Self) - > ; Self
    func>>(lhs:Self,rhs:Self) - > Self
    init(_ val:UInt8)
    }


  2. Declare符合每个整数类型的扩展 - 很容易,因为它们已经实现了 BitshiftOperationsType 中的所有内容:

     扩展Int:BitshiftOperationsType {} 
    扩展Int8:BitshiftOperationsType {}
    扩展Int16:BitshiftOperationsType {}
    扩展Int32:BitshiftOperationsType {}
    扩展Int64: BitshiftOperationsType {}
    扩展UInt:BitshiftOperationsType {}
    扩展UInt8:BitshiftOperationsType {}
    扩展UInt16:BitshiftOperationsType {}
    扩展UInt32:BitshiftOperationsType {}
    扩展UInt64: BitshiftOperationsType {}


  3. 添加一个通用约束,使 T 符合你的新协议:

      func testBytes< T:IntegerType其中T:BitshiftOperationsType>(bytesIn:[UInt8 ],inout dataOut:T){
    让outputSize = sizeof(T)
    var temp:T = 0
    dataOut = 0
    temp = T(bytesIn [0])
    temp = temp<< 1
    }


R.为我在这里得到的总比特数的修正!

I am trying to write a generic function that requires bit shifting operations. I am getting behavior that I don't understand. Here is a simple function that demonstrates the problem.

func testBytes<T: IntegerType>(bytesIn: [UInt8], inout dataOut: T){

let outputSize = sizeof(T)
var temp: T = 0
dataOut = 0
temp = bytesIn[0] as T
temp = temp << 1

}

If I do this, then the last line gives me an error in xcode "T is not convertible to Int".

I can change the last line to temp = temp << (1 as T)

And then the error for this line changes to "T is not convertible to UInt8"

Neither one of these error messages make sense to me in this context. Is there something I can do to enable bit shifting on a generic type?

解决方案

I have a blog post on this topic that goes into more detail, but essentially there are three steps:

  1. Create a new protocol with the bitshift operators and a constructor from UInt8:

    protocol BitshiftOperationsType {
        func <<(lhs: Self, rhs: Self) -> Self
        func >>(lhs: Self, rhs: Self) -> Self
        init(_ val: UInt8)
    }
    

  2. Declare conformance with an extension on each of the integer types - easy since they already implement everything in BitshiftOperationsType:

    extension Int    : BitshiftOperationsType {}
    extension Int8   : BitshiftOperationsType {}
    extension Int16  : BitshiftOperationsType {}
    extension Int32  : BitshiftOperationsType {}
    extension Int64  : BitshiftOperationsType {}
    extension UInt   : BitshiftOperationsType {}
    extension UInt8  : BitshiftOperationsType {}
    extension UInt16 : BitshiftOperationsType {}
    extension UInt32 : BitshiftOperationsType {}
    extension UInt64 : BitshiftOperationsType {}
    

  3. Add a generic constraint so T conforms to your new protocol:

    func testBytes<T: IntegerType where T: BitshiftOperationsType>(bytesIn: [UInt8], inout dataOut: T){
        let outputSize = sizeof(T)
        var temp: T = 0
        dataOut = 0
        temp = T(bytesIn[0])
        temp = temp << 1
    }
    

Thanks to Martin R. for the fix for the gross bit I had here before!

这篇关于在swift通用函数中移位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆