Why is that cast there? Can the try expression return any type other than HackSynth_ZeroAudioUnit? The Type itself is an Obj-C "instancetype", so ought to be a non-optional, yes? It seems like it ought to either be a real instance, or the function ought to throw and the guard never be reached.
Technically objective-C initialiser can return some other class, and I was able creating a situation when "Other path" is taken and the subsequent precondition fails:
func foo() {
let value: AnyObject = try! HackSynth_ZeroAudioUnit(AudioComponentDescription())
print(type(of: value)) // SomeOtherClass
if let value = value as? HackSynth_ZeroAudioUnit {
print("OK")
} else {
print("OTHER") // we are here
}
precondition(value is HackSynth_ZeroAudioUnit) // fails
}
I'm beginning to think this code was written by an intern.
Another thing from the same function, I note that it uses a defer statement to do setup on the View, which assumes the AudioUnit was created, despite the fact that defer runs after throws.
If you rewrite it you may as well get rid of obj-c / obj-c++ in there. Just be careful – it is very easy in swift to do something that allocates memory / blocks on a mutex implicitly – this is no go in realtime context.
Yeah, I'm shaky in this area, so I might be asking for folks to look at what I'm doing as I'm going along. Based on the discussion about real time swift where you pointed out that Apple seemed to be okay with pure swift real time, I'm going to try and make the template all swift, and figure out the buffering and stuff so the DSP kernel can properly be memory move and math.
So, yep, the cast makes sense, but the surrounding bits are dubious.
And really, given that this is a subclass of AUViewController, and AUAudioUnitFactory, it's not clear to be it needs to be declared that way. I can't immediately think of why it shouldn't be declared as HackSynth_ZeroAudioUnit? right off the bat.