Why can't I call the default super.init() on UIViewController in Swift?
The designated initialiser for UIViewController
is initWithNibName:bundle:
. You should be calling that instead.
See http://www.bignerdranch.com/blog/real-iphone-crap-2-initwithnibnamebundle-is-the-designated-initializer-of-uiviewcontroller/
If you don't have a nib, pass in nil
for the nibName (bundle is optional too). Then you could construct a custom view in loadView
or by adding subviews to self.view
in viewDidLoad
, same as you used to.
Error in Swift class: Property not initialized at super.init call
Quote from The Swift Programming Language, which answers your question:
“Swift’s compiler performs four helpful safety-checks to make sure
that two-phase initialization is completed without error:”Safety check 1 “A designated initializer must ensure that all of the
“properties introduced by its class are initialized before it
delegates up to a superclass initializer.”Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks.
https://itunes.apple.com/us/book/swift-programming-language/id881256329?mt=11
Swift: Must call a designated initializer of the superclass error even though code is doing so
The problem there is that you are also trying to access your PreviewNode properties before calling self.init()
Try like this:
Xcode 8 GM • Swift 3
class PreviewNode: SCNNode {
let previewNodeColor: UIColor = .red
let size: CGFloat = 1
let chamferRadius: CGFloat = 0
convenience override init() {
self.init()
let previewBox = SCNBox(width: size, height: size, length: size, chamferRadius: chamferRadius)
previewBox.firstMaterial?.diffuse.contents = previewNodeColor
previewBox.firstMaterial?.transparency = 0.2
previewBox.firstMaterial?.specular.contents = UIColor.white
self.geometry = previewBox
}
}
How do I make a custom initializer for a UIViewController subclass in Swift?
class ViewController: UIViewController {
var imageURL: NSURL?
// this is a convenient way to create this view controller without a imageURL
convenience init() {
self.init(imageURL: nil)
}
init(imageURL: NSURL?) {
self.imageURL = imageURL
super.init(nibName: nil, bundle: nil)
}
// if this view controller is loaded from a storyboard, imageURL will be nil
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
Custom init in Swift Class giving 'property not initialized at implicitly generated super.init call' error?
In swift you most have all variables initialize before super.init unless declared optional, in your case you have two variables that need to be initialized, because you are initializing your variables inside an is statement swift is not sure if it will actually be initialize if the if statement fail.
Change your code to:
var coordinate: CLLocationCoordinate2D?
var title: String?
var subtitle: String?
In case you really don't care if those variables are initialize, or make sure to initialize them with something in the init method.
In the endo you with all variables initialized and/or optional you will be able to call: super.init()
I hope that helped you
Property 'self.*' not initialized at super.init call
You have to initialize all property before you call super.init
in any init method
So,change this before you call super.init()
originView = sourceView //error here
Exception:
- optional property
- property with default value
- lazy property
Related Topics
How to Create Border in Uibutton
Xcode 5.1: Libcordova.A Architecture Problems
Locking a Uisearchbar to the Top of a Uitableview Like Game Center
Uiview Touch Event in Controller
Linking Child View Controllers to a Parent View Controller Within Storyboard
How to Add a Button to Uinavigationbar
Why Is There an Frame Rectangle and an Bounds Rectangle in an Uiview
Why Is My iOS App Not Showing Up in Other Apps' "Open In" Dialog
iOS - 'Myproject-Swift.H' File Not Found When Running Unit Tests for Swift
Cookie Sharing Between Multiple Wkwebviews
Find Current Country from iPhone Device
Dynamically Increase Height of Uilabel & Tableview Cell
Google Maps iOS Sdk: Custom Icons to Be Used as Markers
"No Such Module 'Alamofire'" Xcode Won't Recognize Alamofire Framework
iOS 6.X Open Command Line on Jailbreak
Can't Cast Value of Type Uiviewcontroller to Patterndetailviewcontroller
How to Convert Date Like \/Date(1440156888750-0700)\/ to Something That Swift Can Handle