我在Swift关于初始化器的文档中找到了这个注释:
如果可以使用继承的初始化器满足需求,则不必提供所需初始化器的显式实现。
什么是“显式”实现?那么,什么是“隐含”呢?
“用继承的初始化器满足需求”确切意味着什么?
您能给我一个代码示例吗?在这个示例中,我不需要提供所需初始化器的显式实现?
发布于 2017-07-07 16:36:22
下面是一个带有内联解释的示例:
protocol JSONInitializable { // Use Encoders, but just for example
init(fromJSON: String)
}
class Foo: JSONInitializable {
let x: Int
// "required" is necessary because this init is required for the
// conformance to JSONInitializable
required init(fromJSON json: String) {
//...
x = 123 //some value from the JSON
}
}
class Baz: Foo {
// `init(fromJSON json: String)` can be inherited,
// so it's implicitly defined for Baz, as well as Foo.
}
class Bar: Foo {
// The presence of this uninitialized constant `y` requires an
// a value in the declaration, or an initializer that sets it
let y: Int
// Since we didn't specify a value for `y` in its declaration,
// this initializer must be explicitly specified so as to initialize `y`.
// Doing so blocks the inheritance of `init(fromJSON json: String)` from
// the super class, and requires us to define it ourselves,
// in order to preserve conformance to `JSONInitializable`
required init(fromJSON json: String) {
//...
y = 0
super.init(fromJSON: json)
}
}发布于 2017-07-09 15:14:49
它的意思是:如果在声明属性时初始化了所有属性,则不需要编写初始化程序。
通常,当属性声明而没有设置时,编写init()方法并在那里设置它们,如果父类中有必需的初始化器,则调用
super.init(possible, arg: anotherArg)由于您不需要设置任何内容,也不需要编写任何东西,而且由于您的类中没有init,所以超级调用将自动发生。(当然,如果所需的init需要传入值,则仍然需要提供它们。)那是怎么做到的呢?(见下文)底线是大部分的工作都是为你做的。
但是,一旦编写了init(),就会删除这种自动行为,并且必须确保这些“自动”调用是显式的。
最后,在我举一个例子之前,我应该先谈谈这个声明:
“如果您可以使用继承的初始化器满足此要求,”
如果父程序没有不带参数的初始化程序,那么它将如何获得这些参数?在这种情况下,您需要使用需要的super使用适当的参数初始化类。
下面是一个没有在IB中设置的视图控制器的示例,我通常会使用超级程序所需的初始化器来创建它,因为我没有为派生类编写初始化程序:
设vc = MagicController(nibName: nil,bundle: nil)
import UIKit
import Dotzu
class MagicController: UIViewController, UIGestureRecognizerDelegate {
let action = #selector(MagicController.buttonTapped(_:))
let action2 = #selector(MagicController.secondButtonTapped(_:))
let mainAction = #selector(MagicController.mainButtonTapped(_:))
let defaultPalette = Palette.randomPalette()
var questions = [Question]()
var startTime = TimeInterval()
var endTime = TimeInterval()
var selectedTimeIndex = 0
var selectedTagIndex = 0
var selectedRatingIndex = 0
var selectedTag = "swift"
let timeSpanDropDown = DropDown()
let ratingDropDown = DropDown()
let tagDropDown = DropDown()
var pageNumber = 1
var savedIndex = 0
var savedPostId = -1
var quotaCount = -1
var isFirstTime = true
let queryFactory = Queries(client: APIClient())
var timeSpanButton = UIBarButtonItem()
var ratingButton = UIBarButtonItem()
var tagButton = UIBarButtonItem()
var dbButton = UIBarButtonItem() //
/// start the console log, configure the main view's buttons/actions and present the UI
override func viewDidLoad() {
super.viewDidLoad()
Dotzu.sharedManager.enable()
self.edgesForExtendedLayout = []
configureButtons(container: view)
view.setNeedsLayout()
}
///configure the buttons/actions, prepare and present the UI
/// The first time this is called it sets up the buttons and drop downs
/// cleanupDataSource() has no effect the first time it is called
/// - Parameter animated: passed to super
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if isFirstTime {
isFirstTime = false
initializeButtons()
setupDropDowns()
}
cleanupDataSource()
}
/// etc.
}正如您所看到的,我没有为这个视图控制器编写init,即使视图控制器有一个必需的init。我使用一个甚至不在我的代码中的init()初始化了我的视图控制器。
https://stackoverflow.com/questions/44975479
复制相似问题