可能重复:
带有有序键的 NSDictionary
我在 iOS 上使用 XMPPFramework,当每次插入新的占用者/元素时,包含房间占用者的 NSDictionary 都会改变其内容的顺序,这让我感到非常惊讶。
这是每次插入后的输出。
<CFBasicHash 0xbccbd60 [0x1f02400]>{type = mutable dict, count = 2,entries =>
1 : <CFString 0xbcd90b0 [0x1f02400]>{contents = "4ee90610e4b08c62894a648c"} = <XMPPRoomOccupant: 0xbcd9360>
2 : <CFString 0xbe5a160 [0x1f02400]>{contents = "4eea53a6e4b08c62894a6c90"} = <XMPPRoomOccupant: 0xbca7cc0>
}
<CFBasicHash 0xbccbd60 [0x1f02400]>{type = mutable dict, count = 3,entries =>
0 : <CFString 0xbe60710 [0x1f02400]>{contents = "4ee8fca3e4b08c62894a6489"} = <XMPPRoomOccupant: 0xbe61370>
1 : <CFString 0xbcd90b0 [0x1f02400]>{contents = "4ee90610e4b08c62894a648c"} = <XMPPRoomOccupant: 0xbcd9360>
2 : <CFString 0xbe5a160 [0x1f02400]>{contents = "4eea53a6e4b08c62894a6c90"} = <XMPPRoomOccupant: 0xbca7cc0>
}
<CFBasicHash 0xbccbd60 [0x1f02400]>{type = mutable dict, count = 4,entries =>
0 : <CFString 0xbe5a160 [0x1f02400]>{contents = "4eea53a6e4b08c62894a6c90"} = <XMPPRoomOccupant: 0xbca7cc0>
4 : <CFString 0xbe71eb0 [0x1f02400]>{contents = "4f7ade44e4b09cb64dc33b90"} = <XMPPRoomOccupant: 0xbe72950>
5 : <CFString 0xbe60710 [0x1f02400]>{contents = "4ee8fca3e4b08c62894a6489"} = <XMPPRoomOccupant: 0xbe61370>
6 : <CFString 0xbcd90b0 [0x1f02400]>{contents = "4ee90610e4b08c62894a648c"} = <XMPPRoomOccupant: 0xbcd9360>
}
正如您在最后一个插入中看到的那样,它改变了 2 个元素的顺序,它也改变了它的索引。这是正常的吗?我查看了代码及其插入使用:
[居住者 setObject:occupant forKey:aNickName];
我需要按照插入的顺序存储元素。而且我还需要通过字符串访问元素。任何想法?
谢谢