OC / swift type / structure

----------- OC ---------

 

1. The structure and the same point of the class:

a. plurality of data can be packaged as a whole.

 

1. Different points

 

a. only package data structure, and may also be encapsulated class behavior

 

B. structure variable stack space allocated (if Yes at a local variable), the allocated heap space and the object.

 

                            i stack characteristics: a relatively small space, but the data stored in the stack have a slightly more efficient access

 

                          . Ii stack characteristics: a relatively large space, but the data stored on the heap, some of the inefficiencies

 

                         iii. stored on the stack to be high efficiency of data access, the low efficiency of data stored on the heap have

 

c. Assignment

 

            i. structure (copies)

 

           ii. is between objects (address)

 

              Student body structure

 

       Class: Person

 iii.      

 

2. scenarios

 

a. If the data is encapsulated, there is not only the data, you can only use the class

 

b. If you represent an entity, does not act, there is light attributes,

 

            i. If the property is less, only a few of the case, is defined as a structure, the stack distribution, improve operational efficiency

 

           ii. If the property is more, it is not defined as a structure, such as structure variables will occupy a lot of space in the stack, it will affect the efficiency

----- swift ----

 

1.Swift do not need your custom classes and structures to create a separate interface and implementation files. Swift in, you define a class or structure in a file, the system will automatically generate the code for other external interfaces.
2. Comparison of class structure:
in Swift classes and structures have a lot in common, they can:

  • Custom properties for storing values;
  • A method for providing a defined function;
  • Subscript is used to define the script access values ​​allowed slogan method;
  • Defines the initialization state is used to initialize;
  • It can be extended to the default features that are not;
  • Follow the standard protocol to provide functionality for specific types.

And there is no class structure of additional features:

  • Inheritance allows a class inherits from another class characteristic;
  • Conversion type checking allows you to run and interpret the type of a class instance;
  • Uninitialized instances of a class allows it to release any resources allocated;
  • Reference count to allow more than one reference to a class instance.

3. The structure of the copy is transmitted through your code, and does not use reference counting.

4. Define: Whether you define a new class or structure when, in fact, you define a new type of Swift.

 

The initialization is the simplest syntax or structure of the class name followed by an empty set of parentheses, e.g. Resolution () or VideoMode ().

6. The structural member of the type initializer

 

7. enumerated structure is a value type and
value type when it is a constant or a variable is assigned to, or is passed to the type of the function is copied.
Swift all of the structures, and enumerations are value types, which means that any structure and enumerate instances you have created - and as an example of the type attribute any value contained - always copy the code passed in of.

8. A class is a reference type
relative to the copy, as used herein, is a reference to the existing instance of the same.

9. Occasionally find out whether two variables or constants from the same class useful reference example, in order to allow this, Swift operators provided two features:

  • Identical to (===), means two classes type constant or variable references from the same instance;
  • Not the same as (! ==)

10. The general guidelines, when subject to the following one or more of cases considered to create a structure:

  • The main purpose is to structure the package of some related simple data value;
  • When you are in or pass impart structural example, the package needs to reason data is copied rather than a reference value;
  • Any attributes stored in the structure is a value type, will also be copied, rather than being referenced;
  • Structure does not need to inherit attributes from a type already present, or behavior.

11. strings, arrays, and the dictionary assignment and copies the behavior
of Swift String, Array, and Dictionary types are implemented as a structure, which means strings, arrays, and dictionaries is assigned to a new constant or variable thereof, also, or themselves when they are passed to a function or method, it is actually a copy of transfer.

12. In the related strings, arrays, and dictionary "copy" of the description. You have seen in the code of behavior always seems to copy. However, only if required to do so before the actual copy in the background Swift. Swift is able to manage copies of all of the values ​​to ensure optimal performance, all you do not need to avoid in order to ensure optimal performance of the assignment.

swift: Address reprint articles

https://www.jianshu.com/p/e5a8a3fd407d

 

Published 49 original articles · won praise 7 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_29680975/article/details/102699511