Class Has No Initializers Swift

Class has no initializers Swift

You have to use implicitly unwrapped optionals so that Swift can cope with circular dependencies (parent <-> child of the UI components in this case) during the initialization phase.

@IBOutlet var imgBook: UIImageView!
@IBOutlet var titleBook: UILabel!
@IBOutlet var pageBook: UILabel!

Read this doc, they explain it all nicely.

Swift: XCTest Class 'FirstDemoTests' has no initializers

I understand that you'd like to avoid implicitly unwrapping, but unfortunately (unless you want to go through the trouble of writing your own test runner), you don't have control over which initializer is called when the tests are run.

You could get around this by implicitly unwrapping, as you know, and setting the value of the property in setUp(), which gets called before every test, so you can be sure the property will not be nil during your tests. This is relatively safe.

class MyTests: XCTestCase {

var viewController: ViewController!

override function setUp() {
super.setUp()
self.viewController = ViewController()
}
}

Alternatively, you could make the property lazy, which means that it does not have to be implicitly unwrapped. This means that the property does not have to be initialized during object initialization so you don't need to write a new initializer, but will be initialized the first time it is accessed.

class MyTests: XCTestCase {

lazy var viewController: ViewController = {
return ViewController()
}()
}

Swift - Class has no initializer

Adding to the answer of @Enix and @Shailesh,
I can see you are passing the data to nextViewController, after presenting the viewController,

Hence I don't think you will get the value in the nextVC, always you will get nil there.

So pass the data before presenting the viewController.

Code to replace

self.presentViewController(nextViewController, animated: true, completion: nil)
nextViewController.friend = friendArray[indexPath.row];

BY

nextViewController.friend = friendArray[indexPath.row];
self.presentViewController(nextViewController, animated: true, completion: nil)

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?
}

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.



Related Topics



Leave a reply



Submit