明確化: 問題は、変更可能か不変かについてではなく、正しいクラスのインスタンスを作成するスーパーへの呼び出しについてです。[[self class] alloc]
基本クラスで使用することは考えていませんでしたが、これで問題が解決するようです。何も良いことがない場合は、次の数時間でその anser を受け入れます :)
If a subclass inherits NSMutableCopying from its superclass and declares
additional instance variables, the subclass has to override mutableCopyWithZone:
to properly handle its own instance variables, invoking the superclass’s
implementation first.
それは非常に紛らわしいです。検討
@interface Base : NSObject<NSMutableCopying>
@property (nonatomic, assign) NSInteger value ;
@end
@implementation Base
...
@end
@interface Derived : Base<NSMutableCopying>
@property (nonatomic, assign) NSInteger value2 ;
@end
@implementation Derived
- (id) mutableCopyWithZone: (NSZone *) zone {
// Huh ???
Derived * derived = [super mutableCopyWithZone: zone] ;
...
// Huh ??????
derived.value2 = self.value2 ;
return derived ;
}
...
@end
仕様に従った場合、このコードがどのように正しい可能性があるのか わかりません。
への呼び出しが[super mutableCopyWithZone: zone]
返されたとき、基本クラスは独自の ivar に十分なスペースしか割り当てていないと予想しています。Derived
インスタンスが独自の ivar 用により多くのスペースを必要としていると判断する方法はありません。
ドキュメントが意味することは、実際には何ですか? これをどのように実装すればよいですか?