Search code examples
objective-cnsmutableset

Is [NSMutableIndexSet copy] behavior incorrect?


Look at following two statements:

NSMutableIndexSet *selectedRows = [[[NSMutableIndexSet alloc] initWithIndexSet:[_dataTableView selectedRowIndexes]] autorelease];
NSMutableIndexSet *rowsToDelete = [[selectedRows copy] autorelease];

I expected rowsToDelete to be an NSMutableIndexSet, but its NSIndexSet. Looks like copy is not implemented in NSMutableIndexSet. If thats so, isn't it a big miss not implementing copy even for such a basic type? or I am missing something fundamental to IndexSet interface?


Solution

  • Yes, the behavior is correct.

    You are probably confused by the difference between copy and mutableCopy.

    copy returns the object returned by copyWithZone:. That in turn will return an immutable variant if the class has both an immutable and an mutable variant.

    See the discussion in the documentation for copyWithZone::

    The copy returned is immutable if the consideration “immutable vs. mutable” applies to the receiving object; otherwise the exact nature of the copy is determined by the class.