Swift class and struct 004-class and struct basic use

Source: Internet
Author: User

//: Playground-noun:a Place where people can playImport UIKit//----class and struct------////1. Definition of structurestructSize {//properties in the struct, given default valuesvar width =375var height=667        //methods can be defined in Swift's structure, unlike C, OCfunc Show () {print ("(\ (width): \ (height))")}}var size= Size ()//Construction of a struct to construct an instance of a struct bodyprint (size.width) print (size.height) size.show ()//2. Definition of ClassclassPerson {//the properties defined in the class must be initializedvar name:string =""var age:int=0Let sex:int=0        //definition of a method in a classFunc speak () {print ("Say hello name:\ (name), age:\ (age), sex:\ (Sex)")            }    }//Create an instanceLet Kathy =Person () kathy.speak () Kathy.name="Kathy"Kathy.age=Ten//kathy.sex = 1Kathy.speak ()//3. A property in a class can be a struct-body variableclassView {var opacity=1.0var name=""var viewsize=Size ()}var MyView=View ()//Swift allows setting child properties of struct properties in a classMyView.viewSize.width =480//4. The difference between a struct and a class//(1) The structure has a corresponding one-through constructorvar size2 = Size (width: the, Height: -) size2.show ()//and there is no corresponding constructor in the classvar myView2 =View ()//(2) structs and enumerations are value types, and classes are reference typesvar size3 =Size2size2.width=480size2.show () size3.show () var mark=Person () Mark=Kathy//Mark.speak ()//Kathy.speak ()Kathy.age = -Kathy.speak () mark.speak ( )//The memory management of the instance of the class still uses the Arc//5.=== operator: Used to determine whether a variable or constant refers to the same instance, only for reference types//!== operatorifMark = =Kathy {print ("referencing the same instance")}//= = = and ==,== The value of two instances is equal, = = = Whether the same instance is referenced

Swift class and struct 004-class and struct basic use

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.