Swift 3.0 basic learning and structures

  • 2020-05-24 06:18:25
  • OfStack

preface

Unlike other languages, Swift does not need to create interfaces and implementation files for custom classes and structs. You only need to create a single file to create the classes and structs, and the code system for other external interfaces will be generated automatically. The following article mainly introduces the content of Swift 3.0 class and structure. If you are interested, please take a look.

Class and struct distinction

Swift's classes and structs share the following characteristics:

You can define properties to hold values You can define methods to provide functionality Subscripts can be defined to use their values Initializers can be defined to configure their initialization state You can extend their functionality on the default implementation Follow protocol to provide standard functionality

Class has additional functionality that structs do not:

Inheritance allows one class to inherit the properties of another Type conversion allows you to check and interpret the type of an instance of a class at run time The destructor allows the release of all instance resources that have been assigned to the class Reference counting allows multiple references to an instance of a class

Struct 1 is generally copied directly when assigning values, without reference counting mechanisms.

Symbol definition

Here is an example of defining a struct and class:


struct Resolution {
 var width = 0
 var height = 0
}
class VideoMode {
 var resolution = Resolution()
 var interlaced = false
 var frameRate = 0.0
 var name: String?
}

The structure can be initialized directly


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

Unlike a class, which does not have a default member-by-member initializer.

Structs and enums are value types


let hd = Resolution(width: 1920, height: 1080)
var cinema = hd

To assign a value


cinema.width = 2048

The results of


print("cinema is now \(cinema.width) pixels wide")
// Prints "cinema is now 2048 pixels wide"

Yet hd.width is still 1920


print("hd is still \(hd.width) pixels wide")
// Prints "hd is still 1920 pixels wide"

It can be seen that the assignment process made a deep copy.

Enumeration also has the same behavior, as in the following example, the value of rememberedDirection has not changed:


enum CompassPoint {
 case north, south, east, west
}
var currentDirection = CompassPoint.west
let rememberedDirection = currentDirection
currentDirection = .east
if rememberedDirection == .west {
 print("The remembered direction is still .west")
}
// Prints "The remembered direction is still .west"

A class is a reference type

Such as:


let tenEighty = VideoMode()
tenEighty.resolution = hd
tenEighty.interlaced = true
tenEighty.name = "1080i"
tenEighty.frameRate = 25.0

Do the assignment reference


let alsoTenEighty = tenEighty
alsoTenEighty.frameRate = 30.0

The results of


print("The frameRate property of tenEighty is now \(tenEighty.frameRate)")
// Prints "The frameRate property of tenEighty is now 30.0"

identifier

Exactly the same (===) Not exactly the same (! = = =)

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

The same (===) and the same (==) are not the same:

Exactly the same means that constants or variables of two class types point to exactly the same class instance Equals means that two instances are considered to have the same or equal value, and you can define the == operator to determine that the two instances are in some sense equal

Choose to use classes and structs

Since instance 1 of a struct is usually passed by value and class instance 1 is passed by reference, you need to consider whether you should define a class or a struct based on the actual situation.

Use the structure in one or more of the following cases:

The main purpose of a structure is to encapsulate a small number of simple data values for correlation When assigning or passing an instance of a structure, consider whether it is reasonable for the encapsulated value to be copied rather than referenced Any properties stored in a structure are of value type, and they are expected to be assigned or passed as copies rather than references Structs do not need to inherit properties or behavior from other existing types

Take a look at a few good examples of using structs:

Geometry size, can encapsulate width and height properties, both of type Double A method that points to a range of sequential sequences can encapsulate the start and length properties, both of type Int A point in the 3D coordinate system can encapsulate the x, y and z properties, all of which are of type Double

In other cases, define the class and create an instance of the class, managing and passing the reference.

In practice, most custom data structures use classes more than structures.

Assignment and copy behavior for String, Array, and Dictionary

String, Array, and Dictionary are all structs, so assignments are directly copies, while NSString, NSArray, and NSDictionary are classes, so they use references.

Reference:
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html#//apple_ref/doc/uid/TP40014097-CH13-ID82

conclusion


Related articles: