Pointers If you have experience with c,c++ or Objective-c languages, you may know that these languages use pointers to refer to addresses in memory. A Swift constant or variable referring to an instance of a reference type is similar to a pointer in C, which does not directly point to an address in memory, and does not require you to use an asterisk (*) to indicate that you are creating a reference. These references in Swift are defined in the same way as other constants or variables.
class and struct selection in your code, you can use classes and structs to define your custom data types.
However, a struct instance is always passed by value, and class instances are always passed by reference. This means that they apply to different tasks. When you are considering the data structure and functionality of a project, you need to decide whether each data construct is defined as a class or a struct.
In accordance with common guidelines, consider building a structure when one or more of the following conditions are met:
The main purpose of the structure is to encapsulate a small number of related simple data values. It is reasonable to expect that when an instance of a struct is assigned or passed, the encapsulated data will be copied rather than referenced. Any value-type attribute stored in the structure will also be copied, not referenced. A struct does not need to inherit the properties or behavior of another existing type.
The appropriate structural candidates include:
The size of the geometry, encapsulating a width property and the Height property, both of which are of type double. A range of paths that encapsulate a start property and a length property, both of which are of type int. A point in a three-dimensional coordinate system that encapsulates the x,y and Z attributes, all of which are double in type. In all other cases, define a class, generate an instance of it, and manage and pass it by reference. In practice, this means that most of the custom data constructs should be classes, not structs.
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.