Import foundation/* could fail the propagation of the constructor (call) 1. A constructor that might fail can call a generic constructor in the same Class 2. A generic constructor cannot call a possible failure in the same class constructor 3. In a struct, An ordinary constructor can invoke rewrite 1 of the constructor that could fail the constructor in the same struct. Subclasses can override a potentially failed constructor in the parent class with a constructor that might fail or a normal constructor 2. The generic constructor of a subclass cannot call up the parent class's possible failed constructor 3. A possible failed constructor for a subclass can call the parent class Failed constructor */class Person {var name:string = ""//Definition specified constructor init () {}//definition could fail constructor init? (name:string) {if!name.isempty {self.name = name} else {print ("Construction failed") return nil }}}class Man:person {var sex = "male"//constructor overriding parent class with generic constructor override init (name:string) {//By The constructor is a normal constructor, so you cannot call the explicit (?) of the parent class. Constructors that may fail//can invoke the implicit (!) of the parent class. A constructor that might fail or call a normal constructor in the parent class Super.init ()}//defines a possible failure constructor init! (Name:string, sex:string) A possible failure in the {///subclass constructor can call the parent class that could fail the constructor Super.init (name:name) if sex = = "Other" {print ("construct failed") return nil} self.sex = sex}}let p1 = man (name: "") print (p1) Let P2 = Mans (Name: "Rinpe", Sex: "Male") p Rint (P2 = = nil) Let P3 = Mans (Name: "", Sex: "Female") print (P3 = = nil)
Propagation (invocation) and overriding of constructors that may fail in swift