Surely an ordered set is a more-specific case of a set, so why does NSOrderedSet
inherit from NSObject
rather than NSSet
?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
- How can I add media attachments to my push notific
I went through the interface of
NSSet
and you're right, ordered sets appear to satisfy the Liskov substitution principle and could therefor inherit fromNSSet
.There is one little method that breaks this:
mutableCopy
. The return value ofmutableCopy
must be anNSMutableSet
, butNSMutableOrderedSet
should inherit fromNSOrderedSet
. You can't have both.Let me explain with some code. First, let's look at the correct behaviour of
NSSet
andNSMutableSet
:Now, let's pretend
NSOrderedSet
inherits fromNSSet
, andNSMutableOrderedSet
inherits fromNSOrderedSet
:What if
NSMutableOrderedSet
inherited fromNSMutableSet
instead? Then we get a different problem:In Example 1, you wouldn't be able to pass an
NSOrderedSet
into a function expecting anNSSet
because the behaviour is different. Basically, it's a backwards compatibility problem.In Example 2, you can't pass an
NSMutableOrderedSet
into a function expecting anNSOrderedSet
because the former doesn't inherit from the latter.All of this is because
NSMutableOrderedSet
can't inherit from bothNSMutableSet
andNSOrderedSet
because Objective-C doesn't have multiple inheritance. The way to get around this is to make protocols forNSMutableSet
andNSOrderedSet
, because thenNSMutableOrderedSet
can implement both protocols. I guess the Apple developers just though it was simpler without the extra protocols.