If I have a custom class that conforms to something like AVCaptureVideoDataOutputSampleBufferDelegate (called thousands of times), and I know that I want to save only the first image frame and then do something with all the subsequent image frames, then something like this code is (I guess) fairly standard.
public class myFirstClass {
var firstImageProcessed = false
func saveFirstImage() {
// do some stuff
}
func doWorkOnImageFrame() {
// do some stuff
}
public func processImageFrame_delegateMethod() {
switch self.firstImageProcessed {
case true:
self.doWorkOnImageFrame()
default:
self.saveFirstImage()
}
}
}Is there a way to avoid the unnecessary 'switch', in the situation where I know for sure that I only want the first frame? The following obviously doesn't work, but is there a way with assigning functions to a variable that would avoid calling a switch statement 10,000 times just for that very first case?
public class mySecondClass {
var methodToCall = saveFirstImage
func saveFirstImage() {
// do some stuff
self.methodToCall = doWorkOnImageFrame
}
func doWorkOnImageFrame() {
// do some stuff
}
public func processImageFrame_delegateMethod() {
methodToCall
}
}cheers,
Your code was close, this should work, although method calls have more overhead than switch statements so you're probably not optimizing much.
public class mySecondClass {
var methodToCall = mySecondClass.saveFirstImage
func saveFirstImage() {
// do some stuff
methodToCall = mySecondClass.doWorkOnImageFrame
}
func doWorkOnImageFrame() {
// do some stuff
}
public func processImageFrame_delegateMethod() {
methodToCall(self)()
}
}