Hibernia Hibernia - 3 months ago 26
Swift Question

Subclassing another view controller?

I'm following along with an outdated guide on using Apple's AVCam code.


Apple provides a "CameraViewController". The instructor makes a "Camera" VC that subclasses Apple's cameraVC. In Apple's code, because it's working with an AVCamera, there's a preview view that actually shows the camera output. In the video the guy changes the

@IBOutlet weak var previewView : PreviewView //Apple's PreviewView class

and changes it to

var _previewView : PreviewView

he then goes through apple's code and changes all of the previewView's to _previewView, and then in his own ViewController has this :

class hisViewController: ApplesViewController {

@IBOutlet weak var previewView : PreviewView!

override func viewDidLoad() {

self._previewView = previewView



and when he runs it he gets the camera to run. When I run this I get an EXC Breakpoint error : unexpectedly found nil while unwrapping an Optional value

I've hooked up the previewView to my storyboard for sure.

In the guide he's using apple's Objective-C code in a Swift project but now Apple has released the Swift code.

Any ideas how to make this work / why the error is happening?


IBOutlets are not assigned until the view is loaded. You are trying to access an IBOutlet before super.viewDidLoad fire, so the view hasn't appeared on the screen yet, and previewView is nil.

You could put it after super.viewDidLoad and refresh the view if needed depending on the code of the superclass (self.view.layoufIfNeeded())