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
How to Make Class Methods/Properties in Swift
Uicollectionview - Resizing Cells on Device Rotate - Swift
How to Override Trait Collection for Initial Uiviewcontroller? (With Storyboard)
Nsimage to Nsdata as Png Swift
Masking an Image in Swift Using Calayer and Uiimage
How to Generate Large, Ranged Random Numbers in Swift
What Is the Use of "Static" Keyword If "Let" Keyword Used to Define Constants/Immutables in Swift
Use Resources in Unit Tests with Swift Package Manager
Swift: Extending Functionality of Print() Function
How to Make #Selector Refer to a Closure in Swift
Add Subtitle Under the Title in Navigation Bar Controller in Xcode
Padding a Swift String for Printing
Build Error When Trying to Override an Initializer in Xcode 6.3 Beta 3
How to Get the File Creation Date Using Url Resourcevalues Method in Swift 3
Fibonacci Numbers Generator in Swift 3
What Are the Supported Swift String Format Specifiers
What Versions of Swift Are Supported by What Versions of Xcode