美文网首页
(实验)Swift 值和引用

(实验)Swift 值和引用

作者: LoseAnson洛施安森 | 来源:发表于2017-12-15 13:54 被阅读6次

    元组

    import Foundation
    var a1:(key:String,value:String) = ("name","anson")
    var b1 = a1
    a1.value = "tom"
    print(a1)
    print(b1)
    

    输出

    (key: "name", value: "tom")
    (key: "name", value: "anson")
    

    可见元组赋值是值类型传递

    结构体

    struct Student {
    
        var name:String
        var age:Int
    }
    
    var anson = Student(name: "Anson", age: 18)
    anson.age = 18
    var tom = anson
    tom.name = "Tom"
    
    print("\(anson.name) + \(anson.age)" )
    print("\(tom.name) + \(tom.age)" )
    

    输出

    Anson + 18
    Tom + 18
    

    结构体赋值是值类型传递

    class Teacher {
        var name:String = ""
        var age:Int = 0
    }
    
    var zhang = Teacher()
    zhang.name = "zhang"
    zhang.age = 64
    var wang = zhang
    wang.name = "wang"
    print("\(zhang.name) + \(zhang.age)" )
    print("\(wang.name) + \(wang.age)" )
    

    数组

    var nameList:[String] = ["anson","tom","sam"]
    var secondList = nameList;
    secondList.remove(at: 0)
    print(nameList)
    print(secondList)
    

    输出

    ["anson", "tom", "sam"]
    ["tom", "sam"]
    

    可见数组赋值是值类型传递
    其实数组的定义就是结构体

    字典

    var nameDic = ["first":"anson","second":"sam"]
    var secondDic = nameDic;
    secondDic["first"] = "tom"
    print(nameDic)
    print(secondDic)
    

    输出

    ["second": "sam", "first": "anson"]
    ["second": "sam", "first": "tom"]
    

    可见字典赋值是值类型传递
    其实字典的定义就是结构体

    相关文章

      网友评论

          本文标题:(实验)Swift 值和引用

          本文链接:https://www.haomeiwen.com/subject/cfirwxtx.html