Value type VS Class type in Swift

Source: Internet
Author: User
Tags type casting

What are the similarities and differences between the value Type and Class type in iOS, this problem is seen in the public account, it is interesting, here comb.

1. Why should I set the value type in Swift?

Value type in the process of parameter passing, assignment is the copy process, copy "value" is the memory block of the value, compared to the class type, copy is more direct, there is no object in the method call (drinking count, copy method, etc.), so more efficient. Guessing the type of value that Swift introduces is primarily for efficiency.

2. What value types exist for Swift

The common value types in Swift are struct, enum, or tuple, commonly used Array, String, and Dictionary from the first reference type to the value type

3. How value types and class types should be differentiated

If it is a simple collection of small data, or is used in multiple threads, or if you want to save multiple instances, you can use value types, and you should also use class type.

https://developer.apple.com/swift/blog/?id=10

4. What are the similarities and differences between value types and class?

Classes and structures in Swift has many things in common. Both can:

      • Define Properties to store values

      • Define methods to provide functionality

      • Define subscripts to provide access to their values using subscript syntax

      • Define initializers to set up their initial state

      • be extended to expand their functionality beyond a default implementation

      • Conform to protocols to provide standard functionality of a certain kind

For more information, see Properties, Methods, subscripts, initialization, Extensions, and Protocols.

Classes has additional capabilities that structures does not:

      • Inheritance enables one class to inherit the characteristics of another.

      • Type casting enables you to check and interpret the type of a class instance at runtime.

      • Deinitializers enable an instance of a class to free up any of the resources it has assigned.

      • Reference counting allows more than one Reference to a class instance.

  

A value type parameter is passed as a copy procedure, and parameter passing of class type is the process of passing a reference.

In local variables, the memory of the value type allocates the stack memory, the class object allocates memory on the heap, so the value type of memory is usually allocated more efficiently

Value types in multi-threading because there are multiple copies, so the problem of resource competition is avoided

 

5, the container object in Swift is a value type (after the swift2.0 version), if it is a large array of objects when the parameter is passed, whether the copy will reduce the efficiency of it?

  Value types typically mean the copy process during function parameter passing, but there are two cases in swift that do not copy

1) The compiler will automatically optimize to reduce unnecessary copy parameter passing

    

2) using a let-decorated value type means that the value is immutable, such as an array of arrays, after being decorated, the length of the array is immutable, but the contents are mutable.

Let-modified array arrays are not copied during parameter passing

    

Reference: Http://blog.human-friendly.com/swift-arrays-the-bugs-the-bad-and-the-ugly-incomplete

Value type VS Class type in Swift

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.