4 ms·
here is a case that I have to use !, maybe I'm using it wrong, please correct me. so I have this "member" in a class class XXX: UIView { var member: BBB
by billconan 11y ago
here is a case that I have to use !, maybe I'm using it wrong, please correct me.
so I have this "member" in a class
class XXX: UIView
{
var member: BBB
init()
{
super.init()
self.member = BBB(self.frame)
}
}
if I do this, I got the error that self.member is not initialized before super.init
if I move it to before super.init, self.frame isn't available.
my solution to this chicken-egg thing is putting a ? after "member"
- deleted 11y ago[deleted]
- cballard 11y agolet member = BBB() init() { super.init() member.frame = self.frame } ? Note that since this is a UIView subclass, you're actually implementing init(frame:) and init?(coder:), not init(). I usually unify the two by declaring subviews as lets and initializing them in a private setup method.
- stormbrew 11y agoWhile someone replied with a solution to this problem that may work for you, I will say that I do find the rigidity around initializers frustrating.