Last active
November 18, 2020 14:46
-
-
Save jverkoey/defb7f9f3578d5cb3ff3 to your computer and use it in GitHub Desktop.
Enumerating a MIDIPacketList in Swift 2.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Blogged at http://design.featherless.software/enumerate-midipacketlist-in-swift-part-1/ | |
// and http://design.featherless.software/enumerate-midipacketlist-in-swift-part-2/ | |
extension MIDIPacketList: SequenceType { | |
public func generate() -> AnyGenerator<MIDIPacket> { | |
var iterator: MIDIPacket? | |
var nextIndex: UInt32 = 0 | |
return anyGenerator { | |
if nextIndex++ >= self.numPackets { return nil } | |
if iterator != nil { | |
iterator = withUnsafePointer(&iterator!) { MIDIPacketNext($0).memory } | |
} else { | |
iterator = self.packet; | |
} | |
return iterator | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment