//: Playground-noun:a Place where people can playImport UIKit//----constructor-------//role of the constructor: used to initialize instances of classes, structs, enumerations//If you do not explicitly define the constructor, the system automatically generates a//struct: Generates a one -by-member constructor//class: Generates an argument-free constructor, initialized with the initial value of each property//1. Default constructors for classes and structsstructWeather {var temp:double init () {temp= at}}var weather1=Weather () print (weather1.temp)classDog {Let color:string=" White"var name:string="Wong Choy"var age:int=1}let Dog=Dog () print (dog.color) print (dog.name) print (dog.age)//2. Custom ConstructorsstructCity {var name:string?var location:string?var weather:weather?Init (name:string, location:string, weather:weather) {self.name=name Self.weather=Weather Self.location=Location }//init method with external parameter nameInit (CityName name:string, _ location:string, Wea weather:weather) {Self.name=name Self.location=Location Self.weather=Weather}} var city= City (cityname:"Hangzhou","Middle", Wea:weather ()) print (city)
Swift Builder _009-swift Constructor