How can I extend an Array<UInt8> so that I can access its elements as UInt8 in the extending function? I tried extension Array<UInt8> {} which gives the error message that constraints must be specified in a where clause, but doesn't tell where and how that where clause has to be.
How to extend an array of a specific type?
And you can't simply use a [UInt8] array, like so:
let byteArray: [UInt8] = [0, 127, 255]
let secondByte = byteArray[1] // secondByte will be of type UInt8
print(secondByte.dynamicType) // Swift.UInt8?
Otherwise, you can do:
extension Array where T: UnsignedIntegerType {
...
}But you can't do:
extension Array where T == UInt8 {
}So you have to do this:
extension CollectionType where Generator.Element == UInt8 {
subscript (position: Self.Index) -> UInt8 {
return self[position]
}
}which is nothing but reimplementing what is already there for all CollectionTypes with UInt8 elements ... So I can't really understand why you're not simply using a [UInt8] array as it already gives you the ability to "access its elements as UInt8".
EDIT: Oh, I understand what you meant, seing Wallacy's answer and rereading your question.
extension SequenceType where Generator.Element == UInt8 {
func someMessage(){
print("UInt8 Array")
}
}
let a = [UInt8]();
a.someMessage();