价值显示不同的结果

时间:2017-12-28 14:44:54

标签: swift

任何人都可以解释为什么以下代码显示不同的值

<?php $sql: "select devices_type.devices_type_id, devices_type.devices_type_model from devices_type join devices on devices_type.devices_type_id = devices.devices_type_id?>

为什么Obj_B的值为30而Y的值为10。

感谢。

2 个答案:

答案 0 :(得分:1)

来自Apple文档:

// Reference type example
class C { var data: Int = -1 }
var x = C()
var y = x                       // x is copied to y
x.data = 42                     // changes the instance referred to by x (and y)
println("\(x.data), \(y.data)") // prints "42, 42"

复制引用会隐式创建共享实例。在复制之后,两个变量然后引用单个数据实例。

答案 1 :(得分:0)

这是因为基元是基于值的类型,而类是基于引用的。您可以在Apple's blog找到详细说明。

// Value type example
struct S { var data: Int = -1 }
var a = S()
var b = a                       // a is copied to b
a.data = 42                     // Changes a, not b
println("\(a.data), \(b.data)") // prints "42, -1"



// Reference type example
    class C { var data: Int = -1 }
    var x = C()
    var y = x                       // x is copied to y
    x.data = 42                     // changes the instance referred to by x (and y)
    println("\(x.data), \(y.data)") // prints "42, 42"
相关问题