I'd like to add a custom init
method to the Array
class in swift using an extension, but the key is I'd like to add it only to arrays of bytes (UInt8
)
I've currently got something like this:
public class MyData {
}
public func toByteArray(myData: MyData) -> [UInt8]? { // a global function
// code to check if myData is valid, and extract byte array
}
It seems like the more "swifty" thing to do is use a failable initializer, so I'd like to write it like this
extension Array where Element : UInt8 {
init?(myData: MyData) {
// code to check if myData is valid, and extract byte array
}
}
This code doesn't compile though, I get Type 'Element' constrained to a non-protocol type 'UInt8'
I've found other things where you don't extend Array, you instead extend _ArrayType, but this doesn't seem it'll work for initializers