我知道我可以覆蓋散列和isEqual
來檢查2個實例的相等性。如下混淆默認isEqual和散列實現
- (BOOL)isEqual:(id)other
{
if (other == self) {
return YES;
} else if (![super isEqual:other]) { //WHAT is this line mean ?
return NO;
} else {
return <#comparison expression#>;
}
}
- (NSUInteger)hash
{
return <#hash expression#>;
}
好吧,
other == self
檢查兩個對象指針Xcode中有默認的片段和doucument https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/ObjectComparison.html。if
![super isEqual:other]
,這條線是什麼意思?如果超級對象不等於其他,返回NO?那麼它總是會返回NO
,步驟3將不會被執行。
我錯了嗎?
感謝。
這是檢查是否超類的實現中的isEqual:是假的。 – Tim
if![super isEqual:other]:因爲您重寫了方法,因此只需調用基類方法。 –
如果超類是NSObject,那麼它會檢查兩個實例的指針,這將返回false。那麼第3步不會被執行?對 ? –