站点图标 Codeun

Swift 学习笔记- Struct结构体的知识和用法

Table of Contents

Struct

Struct 结构体主要用来对数据结构的打包封装,它有以下特性

代码示例

// 演示一个三维坐标的调整
struct Point {
    var x = 0.0, y = 0.0, z = 0.0

    // 移动到新坐标
    mutating func moveTo(x: Double, y: Double, z: Double) {
        self.x = x
        self.y = y
        self.z = z
    }
}

var point = Point(x: 1.0, y: 2.0, z: 3.0)
point.moveTo(x: 4.0, y: 5.0, z: 6.0)

print("((point.x), (point.y), (point.z))") 

// -----------
// 打印输出为:
// (4.0, 5.0, 6.0)
退出移动版