문제

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?

도움이 되었습니까?

해결책 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.

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top