I have the following class:
class Scanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate {}
It has a delegate method implemented in the class from AVCaptureVideoDataOutputSampleBufferDelegate
:
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection)
{}
Even when I set the function to private
, fileprivate
, or internal
, it still appears in the code-complete in Xcode:
Is there a way to hide this call from outside class use?
2
Answers
One way is to create a private inner class that conforms to the protocol, and have the inner class hold a weak reference to the outer class, in case you need to use members from the outer class in the implementation of the delegate method.
Rather than setting the
sampleBufferDelegate
toself
, you can set it tovideoOutputDelegate
instead.(I think),
AVCaptureVideoDataOutput
holds a weak reference to itssampleBufferDelegate
, sovideoOutputDelegate
here is the only strong reference here that is keeping the object alive.Your problem is somewhat related to the Single Responsibility Principle. Basically your class tries to do one more task than needed – reacting to
AVFoundation
events, in this case thecaptureOutput
method.Recommending to split your class in two, have one class that handles all the
AVFoundation
setup/delegate, and haveScanner
only deal with raw data. This way:Scanner
in other contexts tooAVFoundation
dependency from the scanner, making it more independentAVFoundation
delegate methods.Also, most likely your scanner doesn’t need a
AVCaptureConnection
to do its work, but unfortunately this dependency is leaked into the class due to the delegate. And splitting the responsibilities helps with this problem too.