In Swift 2, what is the major difference between the three array variants:
Can anyone explain this with a real world example?
Overview. The ArraySlice type makes it fast and efficient for you to perform operations on sections of a larger array. Instead of copying over the elements of a slice to new storage, an ArraySlice instance presents a view onto the storage of a larger array.
A contiguous array is just an array stored in an unbroken block of memory: to access the next value in the array, we just move to the next memory address. This means arr is a C contiguous array because the rows are stored as contiguous blocks of memory. The next memory address holds the next row value on that row.
Array is faster than set in terms of initialization. Set is slower than an array in terms of initialization because it uses a hash process. The array allows to store duplicate elements in it. Set doesn't allow to store duplicate elements in it.
Overview. The ContiguousArray type is a specialized array that always stores its elements in a contiguous region of memory. This contrasts with Array , which can store its elements in either a contiguous region of memory or an NSArray instance if its Element type is a class or @objc protocol.
From the docs:
ContiguousArray:
Efficiency is equivalent to that of Array, unless T is a class or @objc protocol type, in which case using ContiguousArray may be more efficient. Note, however, that ContiguousArray does not bridge to Objective-C. See Array, with which ContiguousArray shares most properties, for more detail.
Basically, whenever you would store classes or @objc protocol types in an array, you might want to consider using ContiguousArray
instead of an Array
.
ArraySlice
ArraySlice always uses contiguous storage and does not bridge to Objective-C.
Warning: Long-term storage of ArraySlice instances is discouraged
Because a ArraySlice presents a view onto the storage of some larger array even after the original array's lifetime ends, storing the slice may prolong the lifetime of elements that are no longer accessible, which can manifest as apparent memory and object leakage. To prevent this effect, use ArraySlice only for transient computation.
ArraySlices are used most of the times when you want to get a subrange from an Array, like:
let numbers = [1, 2, 3, 4]
let slice = numbers[Range<Int>(start: 0, end: 2)] //[1, 2]
Any other cases you should use Array
.
Good source on different class of Swift is : http://swiftdoc.org/
Array is very clear so lets talk about other two.
ContiguousArray: http://swiftdoc.org/type/ContiguousArray/
A fast, contiguously-stored array of T.
Efficiency is equivalent to that of Array, unless T is a class or @objc protocol type, in which case using ContiguousArray may be more efficient. Note, however, that ContiguousArray does not bridge to Objective-C. See Array, with which ContiguousArray shares most properties, for more detail.
ArraySlice : http://swiftdoc.org/type/ArraySlice/
The Array-like type that represents a sub-sequence of any Array, ContiguousArray, or other ArraySlice.
ArraySlice always uses contiguous storage and does not bridge to Objective-C.
In short:
ContiguousArray is for more efficiency when T is a class or @objc protocol type ArraySlice is to represent Array in sub part.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With