I have converted an Objective-C script to Swift for forming header of an AAC frame. However the code fails to run in the app and crashes with an EXC_BAD_ACCESS. Copying the code in to playground outputs what I expect with no errors. Not sure where to go with this, because it seems like it should work. I could always use the Objective-C version but that feels like going around the issue.
func adtsDataForPacketLength(packetLength: Int) -> NSData {
let adtsLength = 7
let profile = 2
let freqIdx = 4
let chanCfg = 1
let fullLength = adtsLength + packetLength;
var packet = [UInt8]()
packet.append(UInt8(0xFF))
packet.append(UInt8(0xF9))
packet.append(UInt8(((profile-1)<<6) + (freqIdx<<2) + (chanCfg>>2)))
packet.append(UInt8(((chanCfg&3)<<6) + (fullLength>>11)))
packet.append(UInt8((fullLength&0x7FF) >> 3))
packet.append(UInt8(((fullLength&7)<<5) + 0x1F))
packet.append(0xFC)
return NSData(bytes: packet, length: packet.count)
}