Dan Rosenstark Dan Rosenstark - 9 months ago 39
Swift Question

Extension for Generic Type `UnsafeMutablePointer<UInt8>`

I'd like to create an extension for

that only affects

I understand that these instructions are pertinent, but I'm not sure how:

When you extend a generic type, you do not provide a type parameter list as part of the extension’s definition. Instead, the type parameter list from the original type definition is available within the body of the extension, and the original type parameter names are used to refer to the type parameters from the original definition.

Basically, I'm trying to use this method:

func toSwift(length: Int) -> [Int] {
var retVal : [Int] = []
for i in 0..<length {
return retVal

to act on
without the
as a parameter... is this possible?


This isn't currently possible in Swift (looking for a citation right now).

However, I don't see a reason not to keep this code generic. What do you think of this?

extension UnsafeMutablePointer {
    func toArray(withLength length: UInt) -> [Memory] { //Change "Memory" to "Pointee" in Swift 3
        return Array(UnsafeBufferPointer(start: self, count: Int(length)))