Swift Study Notes-class and structure, swift Study Notes

Source: Internet
Author: User

Swift Study Notes-class and structure, swift Study Notes

// Class and struct

 

Import UIKit

 

// Class and struct

/*

1. enum, struct, String, Array, and Dictionary types all belong to the value transfer type. When assigned to a new constant or variable, a copy of the value is passed, if it is declared as a constant instance, its attribute value cannot be modified, even if the attribute is a variable

2. The class is a reference type, and the instance reference is passed when the value is assigned.

I. Comparison of classes and struct

Classes and struct in Swift have many things in common. The common cause is:

Define attributes for storing values

Define a method to provide functions

Define ancillary scripts for access value

The definition constructor is used to generate the initialization value.

Add features implemented by default through extension

Implement protocols to provide certain standard functions

 

Compared with struct, classes have the following additional functions:

Inheritance allows one class to inherit the features of another class

Type conversion allows you to check and interpret the type of a class instance at runtime

The deconstruct allows a class instance to release any resources it is allocated.

The reference count allows multiple references to a class.

 

2. According to general rules, when one or more of the following conditions are met, consider constructing a struct:

1. The main purpose of this data structure is to encapsulate a small amount of related simple data values.

2. It is expected that the encapsulated data will be copied rather than referenced when the data structure instance is assigned or transferred.

3. The value type attribute stored in the data structure should also be copied rather than referenced.

4. This data structure does not need to inherit another existing type of attribute or behavior.

*/

// Struct and class definitions

Struct Resolution {

Var width = 0

Var height = 0

Var filename = "data.txt"

}

Class VideoMode {

Lazy var delay attribute = Resolution () // this parameter is not created during instance initialization and is only created when the instance is called.

Var resolution = Resolution ()

Var interlaced = false

Var frameRate = 0.0

Var name: String?

}

Let someresolution = Resolution ()

Let somevideomode = VideoMode () // The attribute values of the instance are default values.

Let vga = Resolution (width: 1024, height: 768, filename: "file.txt") // The constructor of the constructor type has members one by one to initialize the attribute values in the new instance, the class instances do not have default members to build one by one!

// A constant instance of the vga. width = 768 constructor type cannot modify the value of any of its attributes.

Var hd = vga // copy the value of hd to vga. Changing the value of hd is irrelevant to vga. They are different struct instances.

Hd. width = 1920

Print ("hd = \ (hd. width) vga = \ (vga. width )")

Let one = somevideomode // attributes of the constant instance of the class can be changed, because the attribute value of the referenced instance is changed, rather than the reference itself

One. resolution = hd

One. interlaced = true

One. frameRate = 25.0

One. name = "1920i"

Let two = one

Two. resolution. width = 2880 // you can access the subattributes of the Instance attributes.

If two = one {// (= ,! = Equal to, not equal to, one and two reference the same instance)

Print ("one's sub-attribute width value = \ (one. resolution. width) two's sub-attribute width value = \ (two. resolution. width )")

Print ("start calling file: \ (two. delay attribute. filename) ") // delay attribute: This attribute is not created during instance initialization and is created only when it is called. (the" lazy "keyword is added before)

}

 

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.