SoFunction
Updated on 2025-04-11

Swift 3.0 basic learning and structure

Preface

Unlike other languages, Swift does not need to create interfaces and implementation files for custom classes and structures. You only need to create a single file to create classes and structures, and the code system for other external interfaces will be automatically generated. The following article mainly introduces the content about Swift 3.0 class and structure. Interested friends, let’s take a look.

Difference between classes and structures

Swift's classes and structures have the following similar characteristics:

  • You can define attributes to save values
  • Methods can be defined to provide functionality
  • Subscripts can be defined to use their values
  • Initializers can be defined to configure their initialization state
  • They can be extended on the default implementation
  • Comply with the protocol to provide standard functionality

Classes have additional functionality that structs do not have:

  • Inheritance allows a class to inherit the characteristics of another class
  • Type conversion allows you to check and interpret the types of a class instance at runtime
  • The destructor allows the release of all instance resources that have been assigned to the class.
  • Reference count allows multiple instances of a class to be referenced

Generally speaking, structures are copied directly when assigned, and there is no reference counting mechanism.

Symbol definition

Here is an example defining structures and classes:

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

When initializing the structure, you can directly

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

This is different from a class. Classes do not have a default member-by-member initializer.

Structures and enums are value types

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

Assign again

 = 2048

result

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

However, it is still 1920

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

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

Enumerations also have the same behavior, as in the following example, the rememberedDirection value has not changed:

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

Class is a reference type

For example:

let tenEighty = VideoMode()
 = hd
 = true
 = "1080i"
 = 25.0

Make assignment references

let alsoTenEighty = tenEighty
 = 30.0

result

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

Identifier

  • Exactly the same (===)
  • Not exactly the same (!===)
if tenEighty === alsoTenEighty {
 print("tenEighty and alsoTenEighty refer to the same VideoMode instance.")
}
// Prints "tenEighty and alsoTenEighty refer to the same VideoMode instance."

Exactly the same (===) and equal to (==) are different:

  • Exactly the same means that two class types have constants or variables pointing to exactly the same class instance
  • Equal means that two instances are considered to be the same or equal. You can define the == operator by yourself to determine that the two instances are equal in a sense.

Select to use classes and structures

Since instances of structures are generally passed by value, while class instances are generally passed by reference, you need to consider whether to define a class or a structure based on the actual situation.

Use structures if one or more of the following situations:

  • The main purpose of the structure is to encapsulate a small number of simple data values ​​of correlation.
  • When assigning or passing an instance of a structure, it is necessary to consider whether it is reasonable to consider whether the encapsulated value will be copied rather than referenced.
  • Any attributes stored in a structure are of value type, and they also expect to be copied rather than referenced when assigned or passed.
  • Structures do not need to inherit attributes or behaviors from other existing types

Take a look at some examples of using structures:

  • The size of the geometric figure can encapsulate width and height attributes, both of which are Double types
  • Methods pointing to the range of continuous sequences can encapsulate start and length attributes, both of which are Int types
  • A point in a 3D coordinate system can encapsulate x, y and z attributes, both of which are Double types

In other cases, please define the class and create the class instance, and use references for both management and passing.

In practice, most custom data structures use classes and rarely use structures.

Assignment and copy behaviors of String, Array and Dictionary

String, Array and Dictionary are all structures, so assignment is directly a copy, while NSString, NSArray and NSDictionary are classes, so references are used.

Refer to the original English text:
/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/#//apple_ref/doc/uid/TP40014097-CH13-ID82

Summarize

The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.