Classes and structs in Swift

Source: Internet
Author: User

1. Class and structure comparison

The classes and structs in Swift have a lot in common:

    • Defining properties for storing values
    • Define methods to provide functionality
    • Defining satellite scripts for accessing values
    • Defining constructors for generating initialization values
    • Extended to add functionality to the default implementation
    • Compliance with protocols to provide standard functionality for a class

Compared to structs, classes have the following additional functions:

    • Inheritance allows one class to inherit the characteristics of another class
    • Type conversions allow you to check and interpret the type of a class instance at run time
    • The destructor allows an instance of a class to release any resources it has been assigned
    • Reference count allows multiple references to a class

2. Definition syntax for classes and structs

Classes and structs are defined in a similar way. We class struct represent classes and structs separately by keyword and, and define their specific contents in a pair of curly braces, with the following code:

class videomode{        = Resolution ()        false        0.0        var name: String?     }    struct  resolution{        0 0             }

Classes and struct-body instances, the code is as follows:

Let someresolution == Videomode ()

3. struct type member-by-constructor

struct-type member-by-Constructor (memberwise initializers for structure Types): All structs have an automatically generated member-by-constructor that initializes the properties of the members in the new struct instance. The initial values for each property in the new instance can be passed through the name of the property to the member-by-constructor, unlike the struct, where the class instance does not have a default member-by-constructor.

The member-by-constructor code for the struct type is as follows:

Let someresolution = Resolution (width:640, Height:)

4. In Swift, all structs and enumerations are value types. This means that their instances, as well as any value type attributes contained in the instance, are copied when passed in the code.

In fact, in Swift, all the basic types: integers, floating-point numbers (floating-point), Boolean values (Booleans), strings (string), arrays (array), and dictionaries (dictionaries) are value types , and are implemented in the background in the form of structs.

Class 4.1 is a reference type

Unlike a value type, a reference type is referenced when assigned to a variable, constant, or passed to a function, and is not a copy. Therefore, the reference is made to the existing instance itself, not to its copy.

5. Identity operator

Because a class is a reference type, there may be multiple constants and variables that reference a class instance at the same time in the background. (This is not true for structs and enumerations.) Because they are value types, their values are always copied when assigned to a constant, variable, or passed to a function. )

It would be helpful to be able to determine whether two constants or a variable references the same class instance. In order to achieve this, Swift has built two identity operators:

    • Equivalent to (= = =)
    • Unequal to (!==)

These two operators are used to detect two constants or whether a variable refers to the same instance:

if teneighty = = = Alsotentighty {    print ("Tentighty and Alsoteneighty refer to the same Resolution instance. " )}// output "teneighty and Alsoteneighty refer to the same Resolution instance."

6. Assignment and copy behavior of the set (Collection) type

In Swift 字符串(String) , 数组(Array) and 字典(Dictionary) types are implemented in the form of structs. This means that when the String,array,dictionary type data is assigned to a new constant (or variable), or when it is passed into a function (or method), their values copy the behavior (the value is passed).

Classes and structs in Swift

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.