Structures and Classes

作者: 宋奕Ekis | 来源:发表于2021-08-03 14:29 被阅读0次

Comparing Structures and Classes

Memberwise Initializers for Structure Types

struct Resolution {
    var width = 0
    var height = 0
}

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

The structure alway has a memberwise initializer, but class dose not have.

Structures and Enumerations Are Value Types

In fact, all of the basic types in swift are value types.

But be careful about the collection types, such as arrays and dictionaries and strings, which use an optimization to reduce the performance cost of copying. It is copied only before the copies are modified, otherwise those copies will share the memory with the original instance.

Classes Are Reference Types

Identity Operators

  • Identical to (===)
  • Not identical to (!==)

Use these operators to check whether two constants or variables refer to the same single instance.

Let’s think!

相关文章

网友评论

    本文标题:Structures and Classes

    本文链接:https://www.haomeiwen.com/subject/xtkevltx.html