<span style= "FONT-SIZE:24PX;" >struct David {var x = 0;//defines a struct, two fields x, y var y = 0;//Initial value//constructor init () defining struct () {//define an empty constructor, real constructor , call time without parameter call here//The constructor is started with Init, the automatic call//[[OC Alloc]init] is not a constructor println ("in Init"); } init (x:int,y:int) {self.x = x;//defines a constructor with 2 parameters, which is called here with a double argument self.y = y; println ("In Init (x:y:)")}//struct has a constructor, no destructor func getcenter ()->int{return (x+y)/2; }//Give the existing point x, y plus offset//obj.addoffset (100,deltay:) mutating func addoffset (deltax:int,deltay:int) {//struct is copy Object, function inside cannot modify variable//need to modify, add keyword mutating,//mutating, can modify constructor internal variable x + = DeltaX; Y + = DeltaY; }};func Testdavid () {///define a struct//struct is defined as the struct's name (parameter name: parameter value) var d = David ();//Call Constructor init () var s = David (x:100, Y:200);//Call Constructor init (x, y) println ("s.x = \ (s.x), s.y = \ (s.y)") Let C = S.getcenter ();//Call destructor Getcenter print ln ("C is \ (c)"); S.addoffset (100, DELTAY:200);//Because the destructor does not return a value, it cannot be assigned to the variable println ("D is: \ (s.x)");} Testdavid ();//test function call </span>
Swift's struct struct type describes the use