Struct 结构体
介绍
结构体类似 js 中的类,可实例化,内部可以定义变量和方法。
官方文档参考结构和类。
例子
swift
struct Student {
static let tableName = "表格名称"
var name = ""
var age: Int? = nil
var chinese: Int? = nil
var math: Int? = nil
var total: Int? = nil
mutating func conputed() {
self.total = self.chinese! + self.math!
}
}
var zhangSan = Student(name: "张三", age: 12, chinese: 10, math: 20)
zhangSan.conputed()
print(zhangSan)