Frage

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?

War es hilfreich?

Lösung 2

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.

Andere Tipps

Copy returns a non mutable copy. If you want to obtain a mutable copy, you have to send the message "mutableCopy" to a class that implements the NSMutableCopying protocol. As you can see in the developer documentation, NSMutableIndexSet conforms to both NSMutableCopying and NSCopying protocols: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableIndexSet_Class/Reference/Reference.html

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top