我在NSMutableDictionary上遇到了问题,我正在通过一个循环来填充这个问题,这个循环使用的是键的自定义对象类型。我已经实现了NSCopying,所以我希望一切都会好起来。看着循环的结果,我注意到键看起来很好,但是值在NULL中来回翻转是很奇怪的。每次迭代时字典的控制台输出:
{
"C#-10" = "<SWPP_BeamModel: 0xc4ba470>";
"D-10" = "<SWPP_BeamModel: 0xc4b5600>";
"C-10" = "<SWPP_BeamModel: 0x1277f810>";
}
// ...ok so far
{
"C#-10" = "<SWPP_BeamModel: 0xc4ba470>";
"C-10" = "<SWPP_BeamModel: 0x1277f810>";
"D#-10" = (null);
"D-10" = "<SWPP_BeamModel: 0xc4b5600>";
}
// ...oops a null
{
"C#-10" = "<SWPP_BeamModel: 0xc4ba470>";
"C-10" = "<SWPP_BeamModel: 0x1277f810>";
"D#-10" = (null);
"D-10" = "<SWPP_BeamModel: 0xc4b5600>";
"E-10" = (null);
}
// ...and another
{
"C#-10" = "<SWPP_BeamModel: 0xc4ba470>";
"C-10" = "<SWPP_BeamModel: 0x1277f810>";
"D#-10" = (null);
"F-10" = (null);
"D-10" = "<SWPP_BeamModel: 0xc4b5600>";
"E-10" = "<SWPP_BeamModel: 0x1277fad0>";
}
// ...another but value for key "E-10" is back!它就这样继续下去。最后的结论是这条线..。
// De-mutablise
_noteBeamsMap = [NSDictionary dictionaryWithDictionary:beamsMap];,从而导致_noteBeamsMap为
{
"D#9" = (null);
G1 = (null);
C14 = (null);
"G-7" = (null);
...怎么回事?
发布于 2013-12-28 11:26:11
结果是,我在自定义key类中实现了isEqual:,但没有实现匹配的hash。简单地说,在这篇有用的博客文章中 hash需要在isEqual:返回YES时返回相同的值。
https://stackoverflow.com/questions/20813987
复制相似问题