Swift Learning Notes: Value types and reference types!

Source: Internet
Author: User

HelloWorld Chapter

Value:

There are two types in Swift: The first is a value type , each instance of that type holds a copy of the data, and the copy is unique to each instance, such as struct (struct), enum (enum), tuple (tuple), which are value types. The second is a reference type , where an instance of that type shares a single copy of the data (at the native level, that is, each instance of the type points to the same address in memory), such as the class is the reference type

If you want to create a new type, should you choose a value type or a reference type? When you use the Cocoa Framework, many NSObject of the APIs are subclasses, then you have to use the reference type, that is class . In other cases, here are some guidance suggestions you can refer to:

Cases where value types are used:

    • ==when using an operator to compare instance data.
    • When you want to copy an instance data separately.
    • When working with data in a multithreaded environment.

Use a reference type (for example class ) for the case:

    • When using an === operator to determine whether two objects refer to the same object instance.
    • When the context needs to create a shared, mutable object.

In Swift,, Array , String Dictionary are value types. They are used in the same way as in C int , where each instance has a piece of data. You do not need to perform a copy of the display to prevent the data from being modified without your knowledge. What's more, you can pass arguments across threads without having to worry about synchronization, because it's safe to pass a value type. With a high level of security in mind, this type of partitioning model can help you write more predictable code in swift.

The test code is as follows:

Value type Test struct a {    var item:int = 10}var x = a () var y = Xy.item = 20x//Array value type var intarray:[int] = [1,2,3]var Barray = in Tarraybarray[2] = 4barrayintarray//String value type var astring:nsstring = "123456" var bstring = astringbstring = "Aswdef" aString// Dictionary value type var adic:dictionary<string,string>=["kingdoms": "Luo Guan Zhong", "Outlaws of the Marsh": "Shi Naian"];var bDic = adicbdic["a"] = "B" Bdicadicadic.removevalueforkey ("Outlaws") adicbdic//reference type class AClass {    var item:int = 7}var AA = aclass () var bb = Aabb.ite m = 10AA

The playground output values are as follows:

Swift Learning Notes: Value types and reference types!

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.