The difference between Swift class and struct

Source: Internet
Author: User

To be able to develop your app early, so you re-learn swift.

Unlike other programming languages, Swift does not require you to create separate excuses and implementation files for classes and structs, and the system automatically generates external interfaces for other code.

The common denominator of classes and structs:

  • Defining properties for storing values
  • Define methods to provide functionality
  • Defining satellite scripts for accessing values
  • Defining constructors for initializing values

Compared to structs, classes have the following additional functions:

  • Inheritance allows one class to inherit the characteristics of another class

1. Constructors
A constructor is a method that creates an instance of a concrete type. The simplest constructor is a method that does not have any argument instances, writing init.

2. Custom Initialization
You can customize the initialization process and optional property types based on the input parameters, or modify the static properties at initialization time. This section will be described in detail.

Initialize parameters
You can provide a subset of parameters when the constructor is defined, defining the type and name of the variable during the custom initialization process.
Initialization parameters have the same function as functions or method parameters

3. Optional type
If you store properties using a custom type that logically allows values to be null-or their values are not initialized in the constructor, or they are allowed to be empty. You can define an attribute of an optional type. The optional type attribute is an auto-initialize value of nil, which indicates that this property is intentionally set to "null" in the constructor.

4. Initialization and inheritance of classes

All storage properties of a class-including those inherited from the parent class-must be set at initialization time.

5. Value types and reference types

A value type ? is when you assign a value or pass a parameter to a function, create a copy and pass the copy past so that the original data is not affected during the function call.

reference type ? In passing parameters, the data itself is passed over so that the original data is affected during the function call.

The difference between Swift class and struct

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.