(作成した)2種類のオブジェクト(PiecesとOthers)を保持するNSMutableOrderedセットがあります。
PieceとOtherはどちらも、次のようにisEqualメソッドをオーバーライドします。
ピース:
- (BOOL)isEqual:(Piece *)object
{
if (([title isEqualToString:[object title]])
&& ([composer isEqualToString:[object composer]])
&& (major == [object major])
&& (tempo == [object tempo])
&& (pieceKey == [object pieceKey])
&& (pieceTime == [object pieceTime]))
return YES;
else
return NO;
}
他の:
- (BOOL)isEqual:(Other *)object
{
if (([title isEqualToString:[object title]])
&& ([subTitle isEqualToString:[object subTitle]])
&& ([description isEqualToString:[object description]])
&& (otherTime == [object otherTime]))
return YES;
else
return NO;
}
また、両方のクラスのハッシュをオーバーライドして、インスタンスごとに一意のハッシュを作成します(ivarのint値を取得して追加することにより)。
私のアプリでは、An Otherがセットから削除され、セットにピースを追加しようとすると、次のようになります。
-[Other isEqual:]: message sent to deallocated instance 0x80d5680
ハッシュメソッドは次のとおりです。
- (NSUInteger)hash
{
NSUInteger prime = 31;
NSUInteger result = 1;
result = prime * (result + [title intValue]);
result = prime * (result + [composer intValue]);
result = prime * (result + major);
result = prime * (result + tempo);
result = prime * (result + pieceKey);
result = prime * (result + pieceTime);
return result;
}
なぜこれが起こっているのか誰かが知っているなら、私は本当に助けになるでしょう。
ありがとう、