Swift personal learning notes – 08: classes and structures


This article is purely a Chinese version《The Swift Programming Language》So most of the content is in the article. This article is my study notes, not a formal and systematic record. For reference only

There are still a lot of incomprehensible and uncertain points below, which I will point out with “doubtful” notes.

Thanks to the Chinese version of the translator, which greatly speeds up Swift’s learning.



original edition:The Swift Programming Language
Chinese version:Swift 3 programming language – classes and structs

Swift – the difference between class and struct
Differences between structs and classes in swift

Comparison of class and struct in swift

Common points:

  1. Property values can be defined for both
  2. You can define member methods
  3. Subscript scripts can be defined
  4. Can define initializers
  5. You can apply extension
  6. You can use protocol


  1. Class can be inherited
  2. Type conversion allows you to check and interpret the type of a class instance at run time
  3. Class has anti initialization method
  4. Class is a reference type, using reference counting technology, when passing, it is a reference passing. The structure is a value type, and the value is transferred when it is transferred

Define and initialize

    struct SomeStructure {
    class SomeClass {

In swift, some defined rules are as follows:

  • Type: upper camel case
  • Method: lower camel case
  • Attribute: lower camel case

When defining a class / structure, you can add value initialization, such as:

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

Pay attention to the aboveString?Type, the system will automatically assignnilValue.

When creating an instance, you can use the“Initialization method”To create an instance of a class or structure. In addition, you can also specify the value of the property directly at the beginning, but this is not the caseNoUsed to initialize a class.

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

Specific time will be followed by the “initialization” chapter (there should be a link here…)

Characteristic operator

Feature operators are limited to the use of classes

  • == and!= Compare two classescontentIs it equivalent
  • ===and!==: indicates whether two classes have the same reference (the same instance)

String, Array, Dictionary

Unlike Objective-C, in swift, these three types usestructural morphologyTo achieve. However, swift will optimize the efficiency of the program itself, so don’t worry about it.

Recommended Today

OC basis

IOS development interview essential skills chart.png What are objects and what are the objects in OC? An object is an instance of a class; Is an instance created through a class, which is generally called an instance object; Common objects in OC include instance objects, class objects, and metaclass objects; What is a class? What […]