Structs vs Classes (value type vs reference type)

Source: Internet
Author: User

We know that classes is a reference type and structs belongs to a value type. This means that when you pass a Class,swift pass is the object's reference, and when you pass a Struct,swift pass is the value of the object.

Write down the following code in playground to see its output and its effects.


Structs vs Classes

The above defines a struct as a class, each of which has only one variable. The code generates a struct and class, saves them to the corresponding variable, assigns them to the second variable, and the final code changes the properties of the second variable.

As we can see, in the struct example, only the second variable has the Foo attribute changed, but under the class example, the properties of the two variables are changed. This is the difference between a reference type and a value type. When you assign ClassA to classb,swift using the same reference, ClassA and ClassB point to the same instance reference. And when you assign Structa to STRUCTB, Swift makes a copy, so there are now two different structs.

Note: Swift has a copy-on-write mechanism that can be witty to copy the value of a struct only when it is needed. That is, Structb=structa does not copy immediately, because the STRUCTA,STRUCTB value is the same at the beginning. Only if you start to change the value, runtime will copy processing.

For further description, we look at:


Reference

When class, structs as a constant, they have another subtle, important, and different place.

Looking back on what we learned before, we know that Var and let define variables and constants separately. When instantiated as a variable, class has the same behavior as a struct. You can change their properties or give them new values. Class is somewhat different from structs when instantiated as a constant. Let's explore what's going on here.

? When class is a constant, you can change its properties, but you cannot reassign the constant class to a different or new class instance.

? When a struct is a constant, you cannot assign a value, and even its properties you cannot change.

You can show us how it works (you'd better do it yourself, so you'll have a deeper impression)


Demos

Because constant structs in Swift is completely immutable, this is one of the reasons why arrays and dictionaries are structs rather than classes.


Structs vs Classes (value type vs reference type)

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.