I'm trying to understand how initialization works in Swift with a subclass of a UIViewController. I thought the basic format was this, but it is throwing errors...
init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
//other code
super.init(nibName: String?, bundle: NSBundle?)
}
Open ImageViewController. swift and add an initializer with name init(coder:image:) . The initializer accepts an NSCoder instance as its first argument and an Image object as its second argument.
A failable initializer creates an optional value of the type it initializes. You write return nil within a failable initializer to indicate a point at which initialization failure can be triggered. ie; if a condition fails, you can return nil . IMPORTANT: In swift, the initializers won't return anything.
Convenience initializers are secondary, supporting initializers for a class. You can define a convenience initializer to call a designated initializer from the same class as the convenience initializer with some of the designated initializer's parameters set to default values.
The UIViewController class defines the shared behavior that's common to all view controllers. You rarely create instances of the UIViewController class directly. Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy.
You're passing the types, not the variables. You have to pass the variables instead.
init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
// Initialize variables.
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
Variables should now be initialized before the call to super.init
init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
// Initialize variables.
super.init() // as required
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With