swift2 - What’s the difference between Array<T>, ContiguousArray<T>, and ArraySlice<T> in Swift? -
in swift 2, major difference between 3 array variants:
- array
- contiguousarray
- arrayslice
can explain real world example?
from docs:
contiguousarray:
efficiency equivalent of array, unless t class or @objc protocol type, in case using contiguousarray may more efficient. note, however, contiguousarray not bridge objective-c. see array, contiguousarray shares properties, more detail.
basically, whenever store classes or @objc protocol types in array, might want consider using contiguousarray instead of array.
arrayslice
arrayslice uses contiguous storage , not bridge objective-c.
warning: long-term storage of arrayslice instances discouraged
because arrayslice presents view onto storage of larger array after original array's lifetime ends, storing slice may prolong lifetime of elements no longer accessible, can manifest apparent memory , object leakage. prevent effect, use arrayslice transient computation.
arrayslices used of times when want subrange array, like:
let numbers = [1, 2, 3, 4] let slice = numbers[range<int>(start: 0, end: 2)] //[1, 2] any other cases should use array.
Comments
Post a Comment