结构体和类
struct Point{
var x:Int
var y:Int
}
let p = Point(10,20)
p = Point(11,12)
p.x = 20
p.y = 30
Class Size{
var x:Int
var y:Int
Init(x:Int,y:Int){ // 这种写法是Class 类不会自动生成Init初始化构造器
self.x = x
self.y = y
}
}
let s = Size(10,20)
s = Size(20,20)
s.x = 10
s.y = 20
总结:值类型,引用类型的let 修饰是不能改变变量的内存空间
扩展: let str = "hello world"
str.append("swift");// 这是错误
let arr = [1,2,3,4]
arr[0] = 2
对象的堆空间申请过程
在Swift 中,创建类的实例对象,要向堆空间申请内存,流程如下
Class._allocating_init()
libswiftCore.dylib:_swift_allocObject_
libswiftCore.dylib:swift_slowAlloc
libsystem_malloc.dylib:malloc
在Mac,iOS 中malloc 分配内存大小总是16的倍数
我们可以通过 class_getInstanceSize(Size.self)
不占用的
方法的本质就是函数
方法和函数都存放在代码段