從不喜歡搞優(yōu)雅高調(diào)的官方開(kāi)場(chǎng)白,多數(shù)都是直接上代碼!
根據(jù)蘋果API的文檔來(lái)看:The key for value. The key is copied (using copyWithZone:; keys must conform to the NSCopying protocol). If aKey already exists in the dictionary, anObject takes its place.OK,直接看代碼:
@interface WKeyCustom : NSObject<NSCopying>
@property (nonatomic, strong) NSString *name;
@end
@implementation WKeyCustom
//這樣做之后,就可以當(dāng)作key傳到NSDictionary中去了
-(id)copyWithZone:(NSZone *)zone
{
WKeyCustom *aCopy = [[WKeyCustom allocWithZone:zone] init];
if(aCopy)
{
[aCopy setName:[self.name copyWithZone:zone]];
}
return aCopy;
}
@end
- (void)doHashTest
{
WKeyCustom *keyCustomA = [[WKeyCustom alloc] init];
keyCustomA.name = @"keyA";
WKeyCustom *keyCustomB = [[WKeyCustom alloc] init];
keyCustomB.name = @"keyA";
NSMutableDictionary *mdict = [[NSMutableDictionary alloc] init];
[mdict setObject:@"keyA" forKey:keyCustomA];
[mdict setObject:@"keyB" forKey:keyCustomB];
NSLog(@"%@", mdict);//這里輸出啥???--(1)
NSString *str = [mdict objectForKey:keyCustomA];
NSLog(@"%@", str);//這里輸出啥???--(2)
}
答案:
(1)//難到不該是只有一個(gè)對(duì)象么?至少期望是只有一個(gè)對(duì)象的。
{
"<WKeyCustom: 0x618000019860>" = keyB;
"<WKeyCustom: 0x618000019a80>" = keyA;
}
(2)//只實(shí)現(xiàn)了copyWithZone,只能保證放的進(jìn)去,卻沒(méi)辦法取出來(lái)
(null)
--------華麗的分割線-----------
如果我給WKeyCustom類增加一個(gè)isEqual方法呢,會(huì)不會(huì)是我們預(yù)期的結(jié)果呢?
- (BOOL)isEqual:(id)object
{
if ([object isKindOfClass:self.class] && [((WKeyCustom *)object).name isEqualToString:self.name])
{
return YES;
}
return NO;
}
你才剛才的輸出結(jié)果是什么?
答案:
{
"<WKeyCustom: 0x618000208b70>" = keyA;
"<WKeyCustom: 0x618000208b90>" = (null);//這是什么鬼?
}
2017-03-03 13:00:47.416 WiOSDemo[63915:1211904] (null)
這個(gè)結(jié)果很出乎意料吧,難到你猜對(duì)是這樣了?好吧,其實(shí)mdict的內(nèi)容是不一定的,多次運(yùn)行試試,每次運(yùn)行的結(jié)果可能都不太一樣,至少在我們的電腦上是這樣的。其實(shí)這是一個(gè)錯(cuò)誤的設(shè)計(jì),這種做法的結(jié)果是undefined。
--------華麗的分割線-----------
我們?cè)俳o我們的WKeyCustom增加一個(gè)方法,讓它實(shí)現(xiàn)我們預(yù)期的效果:
-(NSUInteger)hash
{
return self.name.hash;
}
結(jié)果:
2017-03-03 13:26:33.103 WiOSDemo[64063:1227637] {
"<WKeyCustom: 0x608000016260>" = keyB;
}
2017-03-03 13:26:33.104 WiOSDemo[64063:1227637] keyB
總結(jié)
- 只要key遵循NSCoping協(xié)議,它確實(shí)可以放到NSDictionary里面
- 你不實(shí)現(xiàn)isEqual:和hash方法也不會(huì)出錯(cuò),因?yàn)镹SObject實(shí)現(xiàn)了
- 當(dāng)且僅當(dāng)兩個(gè)對(duì)象類型相同且你認(rèn)為相等(這種相等由你決定)時(shí), isEqual:才返回YES
- 如果isEqual:返回YES,那么hash也必須返回YES;
On the other hand, it is ok to occasionally have different objects with the same hash value, although it’s better if you minimize how often this happens.