You’re so close you can feel it - you’ve created your m4a file (AudioFileCreateWithURL), you’ve written your lovingly crafted MPEG4 AAC packets to it (AudioFileWritePackets), now all you have to do is close the file (AudioFileClose) and you’re done.

Easy.

But wait, what’s this? AudioFileClose failed, returning kAudioFileUnspecifiedError = ‘wht?’, a.k.a. 0x7768743F, a.k.a 2003334207. Why? Closing is easy, what could possibly go wrong? Have you forgotten something? Scanning AudioFile.h, the only function that you’ve never used is AudioFileOptimize. Yeah, you’ve heard of that guy, he optimizes audio files, and maybe an m4a file just can’t exist if it’s not optimal. It’s probably part of the standard. Truth, beauty. Worth a shot.

wht?
wht?
wht?
WTF?

Oh no! It seems impossible! What to do? Easy. Try everything. Close your eyes, let your wave function superimpose exponentially and simply dip into the Uncollapsed Quantum Infinitude of All Possible States. There’s the answer right there - you’d forgotten to set the AudioFile’s magic cookie*.

You duh head. It’s so obvious now.

* In case you’re wondering, the magic cookie is an opaque chunk of data that stops AudioFileClose from returning ‘wht?’.