Class 'Viewcontroller' Has No Initializers in Swift

Class 'ViewController' has no initializers in swift

The error could be improved, but the problem with your first version is you have a member variable, delegate, that does not have a default value. All variables in Swift must always have a value. That means that you have to set it up in an initializer which you do not have or you could provide it a default value in-line.

When you make it optional, you allow it to be nil by default, removing the need to explicitly give it a value or initialize it.

View controller has no initializers

The problem is here:

var cameraCaptureOutput : AVCapturePhotoOutput

since is not optional, must be initialized:

Classes and structures must set all of their stored properties to an appropriate initial value by the time an instance of that class or structure is created. Stored properties cannot be left in an indeterminate state.

You can set an initial value for a stored property within an initializer, or by assigning a default property value as part of the property’s definition.

to solve the problem, you might define such property as optional:

class ViewController3: UIViewController {
let session = AVCaptureSession()
var camera : AVCaptureDevice?
var cameraPreviewLayer : AVCaptureVideoPreviewLayer?
var cameraCaptureOutput : AVCapturePhotoOutput?
}

Can't figure out why I am getting Class ViewController has no initializers Error in Swift iOS App Code

It's because of this line:

var pageViewController: UIPageViewController

You're saying that pageViewController cannot be null, but you don't have an initializer that sets it to a value. Looking at your code, I suspect you want to change it to this:

var pageViewController: UIPageViewController!

I went into more detail on this problem here.

How to resolve this error? - Class 'ViewController' has no initializers

change let lbl_LastName: UILabel! to var lbl_LastName: UILabel!

Class 'ViewController' has no initializers error: xcode Beta 4

From SwiftSoda.com:

Many Swift Programmers are having a little trouble with ViewControllers and Xcode beta 4. The error is viewController is not constructible with ().

But if you look at your View Controller, you will see another error, Class viewController has no initializers. Followed by (replace with whichever outlets you are using) NSTextField has non-optional type ‘NSTextField’.

Setting the IBOutlets with ? the end corrects the problem. After that you will need to change some syntax each time the Outlet’s variable is used with !.

Here is a brief example:

//ViewController Class File:
// view controller swift file for OS X
import AppKit

// you can use () for AnyObject as long as you do some things first.
var mvc = vc()

class vc: NSViewController {
// the key here is "?" after NSTextField to declare an optional
@IBOutlet var myStatusField: NSTextField?
}

//AppDelegate File:
import AppKit

//here we load the view inside the AppDelegate
class AppDelegate: NSObject, NSApplicationDelegate {

@IBOutlet var window: NSWindow!

func applicationDidFinishLaunching(aNotification: NSNotification?) {
// Insert code here to initialize your application

//load and center view in main window
window.contentView.addSubview(mvc.view)
mvc.view.frame = window.contentView.bounds

//here we display some text in myStatusField with *!.stringValue
// ! is used to unwrap the optional.
mvc.myStatusField!.stringValue = "Swift Soda wishes you a good day!"
}
}


Related Topics



Leave a reply



Submit