类和结构体的简单理解

比我说得好比我说得好比我说得好,重要的说三遍

http://blog.csdn.net/hello_hwc/article/details/50102329

下面是自己的理解
1、类赋值

class SomeClass {
    var name : String?
    var age = 25
}

var oneClass = SomeClass()
oneClass.name = "牛大胆"
print(oneClass.age)
print(oneClass.name!)

var twoClass = oneClass
twoClass.name = "狗指导"
print("\(twoClass.name!)+\(oneClass.name!)")

//对象比较用===
if twoClass === oneClass {
    print("两个相等")
}
image.png

可以看出类的赋值var twoClass = oneClass是浅copy,拷贝了指针。

2、结构体赋值

struct SomeStruct {
    var anyClass = SomeClass()
    var company : String?
    var salary = 10000
    
}
var oneStruct = SomeStruct()
oneStruct.company = "百得康"
var twoStruct = oneStruct
twoStruct.company = "双乾"
print("\(oneStruct.company!) -- \(twoStruct.company!)")
image.png

可以看出结构体的赋值var twoStruct = oneStruct是深copy,拷贝了value。

3、基本类型String和NSMutableArray举例

var companyName : String?
companyName = "双乾"
var companyName2 = companyName
companyName2 = "百得康"
print("\(companyName!)--\(companyName2!)")

var nameArray : NSMutableArray?
nameArray = ["双乾","百得康"]
var nameArray2 = nameArray
nameArray2!.add("联康")
print("\(nameArray!)--\(nameArray2!)")
print("%p--%p",nameArray!,nameArray2!)
image.png

只能分享这些了,多的怕写错,以后理解更深了在补充

你可能感兴趣的:(类和结构体的简单理解)