[Swift] study notes (8)-class and structure, swift Study Notes

Source: Internet
Author: User

[Swift] study notes (8)-class and structure, swift Study Notes

1. Definition of classes and struct

<Pre name = "code" class = "html"> struct Resolution {var width = 0 var height = 0} class VideoMode {var resolution = Resolution () var interlaced = false var frameRate = 0.0 var name: String? // Optional String type name}

 

Class: keyword class, struct: struct


2. Create instances of classes and struct (Instances in other languages are called objects .)

let someResolution = Resolution()let someVideoMode = VideoMode()

Isn't that easy? Why isn't there a keyword new .... It is the same as assigning a function to a variable. Yes. That's simple. No keyword is required.


3. the struct has an Attribute-by-attribute constructor.

let vga = Resolution(width:640, height: 480)


4. Use a. Dot to access attributes, including access and assignment.

someVideoMode.resolution.width = 1280


5. struct is a value type, and class is a reference type.


6. The constant operator of the class is equivalent to (=), not equal (! =) Determine whether two variables or constants reference the same instance


7. the pointer does not need to be specified (*) to indicate that it is a memory address reference and does not need to be accessed by specific symbols (->, it is used in the same way as the access attribute (.) point.


8. Differences between classes and struct

Classes and struct in Swift have many things in common. The common cause is:

  • Define attributes for storing values
  • Define a method to provide functions
  • Define ancillary scripts for access value
  • The definition constructor is used to generate the initialization value.
  • Add features implemented by default through extension
  • Complies with the protocol to provide standard functions for a certain type

Compared with struct, classes have the following additional functions:

  • Inheritance allows one class to inherit the features of another class
  • Type conversion allows you to check and interpret the type of a class instance at runtime
  • The deconstruct allows a class instance to release any resources it is allocated.
  • The reference count allows multiple references to a class.

9. Selection of classes and struct

According to general rules, consider constructing struct when one or more of the following conditions are met:

  • Struct is mainly used to encapsulate a small amount of related simple data values.
  • It is expected that the encapsulated data will be copied rather than referenced when a struct instance is assigned or transferred.
  • Any value type attribute stored in the struct will also be copied rather than referenced.
  • Struct does not need to inherit another existing type attribute or behavior.

Appropriate struct candidates include:

  • The ry size encapsulateswidthAttributes andheightAttribute, both of which areDoubleType.
  • Encapsulate a path within a certain rangestartAttributes andlengthAttribute, both of which areIntType.
  • A point in the 3D coordinate system, encapsulatedx,yAndzAttribute, all of which areDoubleType.

In all other cases, define a class, generate an instance of it, and manage and transmit it through reference. In practice, this means that most of the custom data structures should be classes rather than struct.






Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.