arraysswiftcore-datansorderedset

array to NSOrderedSet change size of list to 1


I have a array of 2 element, when i try to convert it to NSOrderedSet it give me a size of only one element

      LOG("\(listTwoElement.count)")

This line give me 2

   LOG("\(NSOrderedSet.init(array: listTwoElement).count)")

this line give me only 1 element

Why conversion to NSOrderedSet change size of array ?

thanks for your help


Solution

  • A Set is an unordered list of unique elements. If you try to put the same element in a set twice you will only get one

    An Array is an order list of elements that needn't be unique. You can have the same element may times in array.

    A OrderedSet is a combination of both (but a subclass of neither). The items are ordered, like an array, but each element needs to be unique.

    Unique element does not (just) mean that they are stored in different locations in memory, but that they are not "equal" in some fundamental sense. Equality can be defined differently for every class. Classes that want to define it, implement the isEqual: and hash method of the NSObjectProtocol. (Hashes of equal objects must be equal, hashes of unequal object do not need to be different).

    Without knowing more about the element you are dealing with it is hard to say why they are being evaluated as equal when you don't expect them to be. I would start will looking at the class's implementation of the isEqual and hash.