Swift learning "SWIFT programming tour---Classes and structures (13)

Source: Internet
Author: User
Tags uppercase letter

Unlike other programming languages, Swift does not require you to create separate interfaces and implementation files for your custom classes and structs. All you have to do is define a class or struct in a single file, and the system will automatically generate external interfaces for other code.

  Note: Typically an instance of a class is called an object. However, in swift, classes and structs are more closely related than in other languages, and most of the features discussed in this chapter can be used on classes and structs. Therefore, we will primarily use instances rather than objects.

  Comparing classes and struct bodies

Classes and structs have the following characteristics:

    • Defining properties for storing values
    • Define methods that provide specific capabilities or capabilities
    • Defines subscript that provides access to values using the following banner method
    • Defines the initialization method for setting the initial state
    • Extended to add functionality to the default implementation
    • Compliance protocol provides some standard functionality

  The class also has functionality that the struct does not have, as follows:

    • inheritance enables a class to have attributes of another class
    • type conversions allow you to check and interpret the type of a class instance at run time
    • Anti-initialization allows a class instance to free any resources it has been assigned
    • reference count allows multiple references to a class

Note: Structs are passed through the code in a way that is copied, so do not use reference counting.

  Definition syntax

Use the class keyword to declare a class, using the struct keyword to declare a struct.

Classstruct

Note: class and struct names begin with an uppercase letter, and the property and method names begin with a lowercase letter.

Examples such as the following

  struct    Resolution { var  width = 0  var  heigth =   class   Videomode { var  resolution = Resolution ()  var  interlaced = false  var  framerate = 0.0  var  name:string?     

In the example above we define a struct named resolution, which describes the pixel resolution of a display. This structure contains two storage properties called width and height. A stored property is a constant or variable that is bundled and stored in a class or struct. When both properties are initialized to integer 0, they are inferred to be of type int. A class named Videomode that describes a particular pattern for a video display. This class contains four storage attribute variables. The first is the resolution, which is initialized to a new instance of the resolution struct, with the resolution property type. The new Videomode instance also initializes the other three properties, each of which is a inteflaced with an initial value of false (meaning "non-interlaced video"). The playback frame rate initial value is 0.0 framerate and the value is the name of the optional string. The Name property is automatically assigned a default value of nil, meaning "No Name value" because it is an optional type.

  Classes and struct-body instances

The definition of resolution struct and Videomode class only describes what is resolution and videomode. They do not describe a specific resolution (resolution) or video mode. To describe a particular resolution or video pattern, we need to create an instance of them. As follows

Let someresolution == Videomode ()

Any class or struct instance created in this way will be initialized to the default value.

  Property access

Use point syntax to access the attributes contained in the instance. The syntax rule is that the instance name is followed by the property name, both by the dot number (.). Connection:

println ("Thewidth of someresolution is \ (someresolution.width)")

You can also access the child properties, how to videomode the width property of the resolution property:

println ("Thewidth of somevideomode is \ (someVideoMode.resolution.width)")

You can also assign a value to a property variable using dot syntax

12880 println ("Thewidth of somevideomode is now \ (someVideoMode.resolution.width)")

Note: Unlike the Objective-c language, Swift allows you to set the child properties of a struct property directly. The last example above is to set the Somevideomode property of the resolution property directly, and the above operation does not need to set the resolution property from the new setting.

struct type of member initializer by individual

All structs have an automatically generated member-by-one initialization method that initializes the properties of the members of the new struct instance. The initial values for each property in the new instance can be passed to the member-by-initializer by the name of the property:

Let VGA = Resolution (width:640480)

Unlike structs, class instances do not have a default member-by-initializer.

  Structs and enumerations are value types

A value type is a value, when it is assigned to a variable or constant, or when it is passed to a function, its value is copied, which means that their instance, and any value type attributes contained in the instance, are copied when passed in the code.

1920x1080  the  var cinema = HD

In the example above, a constant named HD is declared with a value of resolution instance initialized to full HD video resolution (1920 pixels wide and 1080 pixels high).

The example then declares a variable named cinema with a value of the previously declared HD. Because resolution is a struct, cinema's value is actually a copy of HD, not HD itself. Although HD and cinema have the same wide (width) and high (height) properties, they are two completely different instances in the background.

  Class is a reference type

Unlike a value type, a reference type is assigned to a variable, a constant, or is passed to a function, and the operation is not a copy of it. Therefore, referring to an existing instance itself instead of its copy

  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. 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 (!==)

 if teneighty = = = Alsotentighty {     println ("Tentighty and Alsoteneighty refer to The same Resolution instance. " ) }

  Pointer

If you have experience with c,c++ or objective-c language, then you might know that these languages use pointers to refer to addresses in memory. A Swift constant or variable referencing an instance of a reference type is similar to a pointer in C, but not directly to an address in memory, and does not require you to use an asterisk (*) to indicate that you are creating a reference. These references in Swift are defined in the same way as other constants or variables.

Swift learning "SWIFT programming tour---Classes and structures (13)

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.