[英]Where is UIView.init() documented?
In the folowing example: 在以下示例中:
import UIKit
class MyView: UIView
{
override init(frame: CGRect) {
super.init(frame: frame)
print ("...init with frame")
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
print("...init with coder")
}
}
....... .......
let var = MyView() // "...init with frame" gets printed
So it seems MyView()
calls UIView.init()
which in turn calls init(frame: CGRect)
. 因此,似乎
MyView()
调用了UIView.init()
,后者又调用了init(frame: CGRect)
。 However according to the UIView documentation there is only a init(frame:) (and (init:coder)). 但是,根据UIView文档 ,只有一个init(frame:)(和(init:coder))。
According to the documenation for NSObject. 根据NSObject的文档 。 init()
在里面()
does no initialization;
不初始化; it simply returns self.
它只是返回自我。
So there must be a UIView.init()
that overrides NSObject.init()
and in turn calls init(frame:)
. 因此,必须有一个覆盖
NSObject.init()
的UIView.init()
,并依次调用init(frame:)
。 But this UIView.init()
isn't documented? 但是这个
UIView.init()
没有记录吗? Or am I missing this documentation? 还是我缺少此文档?
I have found no documentation for UIView.init
, but UIView.init
does exist. 我没有找到关于
UIView.init
文档,但是UIView.init
确实存在。 If you set a breakpoint at your print ("...init with frame")
, you will see the call to [UIView init]
in the Debug Navigator . 如果在
print ("...init with frame")
设置了一个断点print ("...init with frame")
,您将在Debug Navigator中看到对[UIView init]
的调用。 The assembly code clearly shows it calling objc_msgSend
to initWithFrame:
using CGRectZero
as the frame: 汇编代码清楚地显示出它使用
CGRectZero
作为框架,将objc_msgSend
调用到initWithFrame:
:
UIKit`-[UIView init]:
0x10b3ba160 <+0>: pushq %rbp
0x10b3ba161 <+1>: movq %rsp, %rbp
0x10b3ba164 <+4>: subq $0x20, %rsp
0x10b3ba168 <+8>: movq 0xc52f71(%rip), %rsi ; "initWithFrame:"
0x10b3ba16f <+15>: movq 0xcd5f72(%rip), %rax ; (void *)0x000000010f732690: CGRectZero
0x10b3ba176 <+22>: movq 0x18(%rax), %rcx
0x10b3ba17a <+26>: movq %rcx, 0x18(%rsp)
0x10b3ba17f <+31>: movq 0x10(%rax), %rcx
0x10b3ba183 <+35>: movq %rcx, 0x10(%rsp)
0x10b3ba188 <+40>: movq (%rax), %rcx
0x10b3ba18b <+43>: movq 0x8(%rax), %rax
0x10b3ba18f <+47>: movq %rax, 0x8(%rsp)
0x10b3ba194 <+52>: movq %rcx, (%rsp)
0x10b3ba198 <+56>: callq *0xcd7042(%rip) ; (void *)0x000000010c8b9800: objc_msgSend
0x10b3ba19e <+62>: addq $0x20, %rsp
0x10b3ba1a2 <+66>: popq %rbp
0x10b3ba1a3 <+67>: retq
UIView.init()
exists — however, it's not publicly declared because that declaration is inherited from the NSObject
class. UIView.init()
存在-但是,未公开声明,因为该声明是从NSObject
类继承的。
In neither Swift nor ObjC-bridged-to-Swift does a class have to declare as public API the methods/properties it inherits from a superclass or includes by conforming to a protocol. 在Swift和ObjC-bridged-to-Swift中,类都不必将其从超类继承或通过遵循协议包括的方法/属性声明为公共API。 Inheritance or protocol adoption means that the class supports those methods/properties, but it doesn't tell you anything about what the class' implementation of them does .
继承或协议收养意味着该类支持这些方法/属性,但它不会告诉你是什么类的实现他们做任何事情。 For that you have to read documentation... carefully.
为此,您必须仔细阅读文档。
Expanding a bit on the NSObject.init()
doc you quoted (with some editing for emphasis): 在您引用的
NSObject.init()
文档上扩展一点(进行一些编辑以强调):
Implemented by subclasses to initialize a new object (the receiver) immediately after memory for it has been allocated.
由子类实现,以在为其分配内存后立即初始化一个新对象(接收者)。
The
init()
method defined in theNSObject
class does no initialization... (1)NSObject
类中定义的init()
方法不进行初始化...(1)In a custom implementation of this method, you must invoke
super
's initializer then initialize and return the new object... (2)在此方法的定制实现中,必须调用
super
的初始化程序,然后初始化并返回新对象...(2)
This could probably be clearer ( I recommend filing a bug against the documentation so Apple knows to improve it ), so a quick translation: 这可能更清楚了( 我建议针对文档提出错误,以便Apple知道对此进行改进 ),因此快速翻译一下:
In a "bare" NSObject
instance, init()
does nothing. 在“裸”
NSObject
实例中, init()
不执行任何操作。 That is, the method is there purely for subclassing. 也就是说,该方法仅用于子类化。
Subclasses are expected to invoke super
and perform their own initialization. 子类应该调用
super
并执行自己的初始化。 Where you see "custom" and "you" here, read it as the implementor of a subclass — that, is, the "you" here applies equally to you, the third-party developer creating new classes in your app, and to the authors of other Apple classes that subclass NSObject
. 在此处看到“ custom”和“ you”的地方,将其视为子类的实现者-也就是说,此处的“ you”同样适用于您,在您的应用中创建新类的第三方开发人员以及
NSObject
的其他Apple类的作者。 (You know, just about all of them.) (你知道的,几乎所有的。)
The way Apple's API documentation seems to work, they only have docs at the method level for methods that are declared by a class — there aren't docs on UIView
for all the hundreds of methods that it inherits from all of its superclasses, nor on each of the many UIView
subclasses for all of the methods they inherit from UIView
. Apple的API文档的工作方式似乎是,它们仅在方法级别针对类声明的方法提供文档
UIView
上没有针对其从其所有超类继承的所有数百种方法的文档,从UIView
继承的所有方法的许多UIView
子类中的每个子类。
As noted in other answers, the no-args initializer for UIView
is equivalent to using init(frame:)
and passing CGRect.zero
. 如其他答案中所述,
UIView
的no-args初始化程序等效于使用init(frame:)
并传递CGRect.zero
。 Even though Apple's docs don't have a place to attach descriptions to UIView.init()
, it'd probably be helpful for them to discuss the effects of inherited initializers somewhere else. 即使Apple的文档没有将描述附加到
UIView.init()
,对于他们在其他地方讨论继承的初始化程序的影响也可能会有所帮助。 (Maybe in the UIView
class overview ?) That's probably worth filing another documentation bug. (也许在
UIView
类概述中 ?)可能值得提出另一个文档错误。
If you call MyView() in swift. 如果您迅速调用MyView()。 It means it call default constructor ie init is called.
这意味着它将调用默认构造函数,即调用了init。 If you want to call initWithFrame as in objective c then you have to call constructor like MyView(frame:CGRect).
如果要在目标c中调用initWithFrame,则必须调用MyView(frame:CGRect)之类的构造函数。 And for detail you must follow apple documentation.
有关详细信息,您必须遵循Apple文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.