In Swift, all the basic types: integers (integer), floating-point numbers (floating-point), Boolean (Boolean), string, array, and dictionary (dictionary) are value types , and is implemented in the form of structs at the bottom. The class is a reference type .
1. Test if the array is a value type
var Testarray =[String] () testarray.append ("AA") Testarray.append ("BB") Testarray.append ("CC") var testArray2=Testarrayprint ("testarray:\ (Testarray)") Print ("testarray2:\ (testArray2)") Testarray2.removeall () print ("after the RemoveAll operation is complete----") Print ("testarray:\ (Testarray)") Print ("testarray2:\ (testArray2)")
Results:
2. Test if the dictionary is a value type
var testdic =[string:string] () Testdic.updatevalue ("AA", Forkey:" One") Testdic.updatevalue ("BB", Forkey:" A") Testdic.updatevalue ("CC", Forkey:" -") Testdic.updatevalue ("DD", Forkey:" -") Print ("testdic:\ (testdic)") var testDic2=Testdicprint ("testdic2:\ (TESTDIC2)") Testdic2.removeall () print ("after the removeall operation is complete-----") Print ("testdic2:\ (TESTDIC2)") Print ("testdic:\ (testdic)")
Results:
3. Test if the class is a reference type
classPerson {var name:string?var age:int= -func SayHello () {print ("Hello, my name is \ (name), my age was \ (age)")}}var p1=Person () var p2=P1print ("p1:\ (p1)") Print ("p2:\ (p2)")//SayHelloPrint"SayHello:") P1.sayhello () P2.sayhello ()//Modify the name of P1Print"Modify the name of the P1:") P1.name="Zhang San"P1.sayhello () P2.sayhello ( )
Test:
Arrays and dictionaries in Swift are value types