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)
.
However according to the UIView documentation there is only a init(frame:) (and (init:coder)).
According to the documenation for 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:)
. But this UIView.init()
isn't documented? Or am I missing this documentation?
The UIView class is a concrete class that you can instantiate and use to display a fixed background color. You can also subclass it to draw more sophisticated content.
init(coder:)Returns an object initialized from data in a given unarchiver. iOS 2.0+ iPadOS 2.0+ macOS 10.0+ Mac Catalyst 13.0+ tvOS 9.0+ watchOS 2.0+ Required.
I have found no documentation for UIView.init
, but UIView.init
does exist. If you set a breakpoint at your print ("...init with frame")
, you will see the call to [UIView init]
in the Debug Navigator. The assembly code clearly shows it calling objc_msgSend
to initWithFrame:
using CGRectZero
as the frame:
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
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