var arraya = [1,2,3,4]
var arrayb = arraya
if arraya === arrayb
{
println("arraya is identical to arrayb")
}
else
{
println("arraya is not identical to arrayb")
}
Why the xcode print "arraya is not identical to arrayb"?
var arraya = [1,2,3,4]
var arrayb = arraya
if arraya === arrayb
{
println("arraya is identical to arrayb")
}
else
{
println("arraya is not identical to arrayb")
}
Why the xcode print "arraya is not identical to arrayb"?
Well, it looks like a bug.
Arrays are value types in swift, but copying behaviour slightly different than the other value types like enums,dictionaries.
For arrays, copying only takes place when you perform an action that has the potential to modify the length of the array.
It means that if you want an operation that could change the length of array, copying takes place. Like you add or remove an item, replacing items. In your case, you' re just assigning your array to a new variable. I don't think it's the potential to change the length of array.
After inspecting the functions headers a bit, I realised that
The ===
operator is normally defined only for objects (AnyObject
)
Array
is a struct but they have added a ===
operator for Arrays, too
/// Returns true iff these arrays reference exactly the same elements.
func ===<T : ArrayType, U : ArrayType>(lhs: T, rhs: U) -> Bool
In my understanding that should be true
for your example, so it actually might be be a bug.
EDIT: This has been fixed in DP2