1.结构体对象
struct Resolution {
var width = 0
var height = 0
}
var one = Resolution()
print("one地址为: \(Unmanaged<AnyObject>.passUnretained(one as! AnyObject).toOpaque())")
var two = one
print("two地址为:\(Unmanaged<AnyObject>.passUnretained(two as! AnyObject).toOpaque())")
结果如下
one地址为: 0x000060800004e1f0
two地址为: 0x000060800004e1c0
结论
在swift 中结构体中的复制操作是结构体对象的copy 重新拷贝了一份新的结构体对象
2.类对象
class VideoMode {
var resolution = Resolution()
var interlaced = false
var frameRate = 0.0
var name: String?
}
var three = VideoMode()
print("three地址为: \(Unmanaged<AnyObject>.passUnretained(three as! AnyObject).toOpaque())")
var four = three
print("four地址为: \(Unmanaged<AnyObject>.passUnretained(four as! AnyObject).toOpaque())")
结果如下
three地址为: 0x000060000008dac0
four地址为: 0x000060000008dac0
结论
类对象的之间赋值操作,只是添加了一个引用,没有创建新的对象.
SuperD总结
1.结构体中的结构体对象之间赋值操作会产生一个新的对象
2.类对象中的类对象之间的复制操作不会产生新的对象,只是添加了一个指针指向
网友评论