本文介绍了为什么NSOrderedSet不从NSSet继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当然,有序集是一个更具体的集合,为什么 NSOrderedSet 继承自 NSObject 而不是 NSSet

Surely an ordered set is a more-specific case of a set, so why does NSOrderedSet inherit from NSObject rather than NSSet?

推荐答案

我经历了<$的界面c $ c> NSSet 你是对的,有序套装似乎满足并且可以继承自 NSSet

I went through the interface of NSSet and you're right, ordered sets appear to satisfy the Liskov substitution principle and could therefor inherit from NSSet.

有一个小方法可以打破这个: mutableCopy mutableCopy 的返回值必须是 NSMutableSet ,但 NSMutableOrderedSet 应该从 NSOrderedSet 继承。你不能同时拥有这两个。

There is one little method that breaks this: mutableCopy. The return value of mutableCopy must be an NSMutableSet, but NSMutableOrderedSet should inherit from NSOrderedSet. You can't have both.

让我解释一些代码。首先,让我们看一下 NSSet NSMutableSet 的正确行为:

Let me explain with some code. First, let's look at the correct behaviour of NSSet and NSMutableSet:

NSSet* immutable = [NSSet set];
NSMutableSet* mutable = [immutable mutableCopy];

[mutable isKindOfClass:[NSSet class]]; // YES
[mutable isKindOfClass:[NSMutableSet class]]; // YES

现在,让我们假装 NSOrderedSet 继承自 NSSet NSMutableOrderedSet 继承自 NSOrderedSet

Now, let's pretend NSOrderedSet inherits from NSSet, and NSMutableOrderedSet inherits from NSOrderedSet:

//Example 1
NSOrderedSet* immutable = [NSOrderedSet orderedSet];
NSMutableOrderedSet* mutable = [immutable mutableCopy];

[mutable isKindOfClass:[NSSet class]]; // YES
[mutable isKindOfClass:[NSMutableSet class]]; // NO (this is the problem)

如果 NSMutableOrderedSet 继承自 NSMutableSet ?然后我们得到一个不同的问题:

What if NSMutableOrderedSet inherited from NSMutableSet instead? Then we get a different problem:

//Example 2
NSOrderedSet* immutable = [NSOrderedSet orderedSet];
NSMutableOrderedSet* mutable = [immutable mutableCopy];

[mutable isKindOfClass:[NSSet class]]; // YES
[mutable isKindOfClass:[NSMutableSet class]]; // YES
[mutable isKindOfClass:[NSOrderedSet class]]; // NO (this is a problem)

在例1中,你将无法通过一个 NSOrderedSet 进入一个期望 NSSet 的函数,因为行为不同。基本上,这是一个向后兼容性问题。

In Example 1, you wouldn't be able to pass an NSOrderedSet into a function expecting an NSSet because the behaviour is different. Basically, it's a backwards compatibility problem.

在示例2中,您不能将 NSMutableOrderedSet 传递给函数期待 NSOrderedSet 因为前者不会继承后者。

In Example 2, you can't pass an NSMutableOrderedSet into a function expecting an NSOrderedSet because the former doesn't inherit from the latter.

所有这一切都是因为 NSMutableOrderedSet 无法从 NSMutableSet NSOrderedSet 继承,因为Objective-C没有多重继承。解决这个问题的方法是为 NSMutableSet NSOrderedSet 制定协议,因为那时 NSMutableOrderedSet 可以实现这两种协议。我猜Apple开发人员只是在没有额外协议的情况下更简单。

All of this is because NSMutableOrderedSet can't inherit from both NSMutableSet and NSOrderedSet because Objective-C doesn't have multiple inheritance. The way to get around this is to make protocols for NSMutableSet and NSOrderedSet, because then NSMutableOrderedSet can implement both protocols. I guess the Apple developers just though it was simpler without the extra protocols.

这篇关于为什么NSOrderedSet不从NSSet继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 20:13