[Swift]陣列元素是class和struct時的引用測試

weixin_34148340發表於2018-12-27

結論寫在最前面:

陣列元素是struct時,複製陣列後修改,以及修改指向陣列元素的變數都不會影響到原來的陣列;元素是class時則會。
詳細的測試用例一圖清楚明瞭。


13464949-378d7d71275b7ed9.png
測試結果

測試程式碼

    class Person : CustomDebugStringConvertible {
        
        var debugDescription: String {
            return "debug:\(age)"
        }
        
        var age : Int = 0
        init(age:Int){
            self.age = age
        }
    }
    
   func forClass() {
        var perArr = [Person]()
        perArr.append(Person(age:12))
        perArr.append(Person(age:13))
        perArr.append(Person(age:14))
        
        print("A:perArr = \(perArr)")
        //perArr = [debug:12, debug:13, debug:14]
        
        var copyArr = perArr
        print("A:copyArr = \(copyArr)")
        //A:copyArr = [debug:12, debug:13, debug:14]
        
        //==============[case 1]=======================
        //操作:對陣列複製後,修改複製得到的陣列的元素,
        copyArr[1].age = 100
        print("B:copyArr = \(copyArr)")
        //B:copyArr = [debug:12, debug:100, debug:14]
        print("B:perArr = \(perArr)")
        //B:perArr = [debug:12, debug:100, debug:14]
        //結果:原來陣列的元素也會發生改變,兩個輸出結果裡都是'debug:100';所以陣列元素依然是引用
        
        //==============[case 2]=======================
        //操作:新建一個元素,把元素新增到陣列後再修改該元素
        let testP = Person(age:999)
        copyArr.append(testP)
        print("C:copyArr = \(copyArr)")
        //C:copyArr = [debug:12, debug:100, debug:14, debug:999]
        print("A:testP = \(testP)")
        //A:testP = 999
        testP.age = 888
        print("D:copyArr = \(copyArr)")
        //D:copyArr = [debug:12, debug:100, debug:14, debug:888]
        //結果:陣列的元素也會發生改變,輸出結果裡有'debug:888';所以陣列元素依然是引用
    }
   struct User : CustomDebugStringConvertible {
        var age : Int = 0
        var debugDescription: String {
            return "debug:\(age)"
        }
    }

    func forStruct() {
        var perArr = [User]()
        perArr.append(User(age:12))
        perArr.append(User(age:13))
        perArr.append(User(age:14))
        
        print("A:perArr = \(perArr)")
        //perArr = [debug:12, debug:13, debug:14]
        
        var copyArr = perArr
        print("A:copyArr = \(copyArr)")
        //A:copyArr = [debug:12, debug:13, debug:14]
        
        //==============[case 1]=======================
        //操作:對陣列複製後,修改複製得到的陣列的元素,
        copyArr[1].age = 100
        print("B:copyArr = \(copyArr)")
        //B:copyArr = [debug:12, debug:100, debug:14]
        print("B:perArr = \(perArr)")
        //B:perArr = [debug:12, debug:13, debug:14]
        //結果:原來陣列的元素不會發生改變,兩個輸出結果裡都是'debug:100'和‘debug:13’
        
        //==============[case 2]=======================
        //操作:新建一個元素,把元素新增到陣列後再修改該元素
        var testP = User(age:999)
        copyArr.append(testP)
        print("C:copyArr = \(copyArr)")
        //C:copyArr = [debug:12, debug:100, debug:14, debug:999]
        print("A:testP = \(testP)")
        //A:testP = 999
        testP.age = 888
        print("D:copyArr = \(copyArr)")
        //D:copyArr = [debug:12, debug:100, debug:14, debug:999]
        //結果:陣列的元素不會發生改變,輸出結果裡還是'debug:999';
    }

相關文章