从 PList 读取字典时出现错误顺序
我有一个 plist,我试图从中按顺序访问其动画(如 PList 中所定义),但是当我将其存储在 NSDictionary 中,然后尝试一个接一个地播放动画时,它不会按顺序播放它们。 例如,
int index = 0;
[self playAnimation:[animTypes objectAtIndex:index++]];
[self playAnimation:[animTypes objectAtIndex:index++]];
这是我的 Plist 的代码:
<plist version="1.0">
<dict>
<key>Boy</key>
<dict>
<key>Enter</key>
<dict>
<key>disabled</key>
<false/>
<key>halfCycle</key>
<true/>
<key>fps</key>
<integer>1</integer>
<key>defaultFrame</key>
<integer>0</integer>
<key>useZwoptex</key>
<true/>
<key>frames</key>
<array>
<string>boy-000.png</string>
</array>
</dict>
<key>Jump</key>
<dict>
<key>disabled</key>
<false/>
<key>halfCycle</key>
<true/>
<key>fps</key>
<integer>13</integer>
<key>defaultFrame</key>
<integer>0</integer>
<key>useZwoptex</key>
<true/>
<key>frames</key>
<array>
<string>boyjump-000.png</string>
<string>boyjump-001.png</string>
<string>boyjump-002.png</string>
</array>
</dict>
<key>Turnaround</key>
<dict>
<key>disabled</key>
<false/>
<key>halfCycle</key>
<true/>
<key>fps</key>
<integer>21</integer>
<key>defaultFrame</key>
<integer>0</integer>
<key>useZwoptex</key>
<true/>
<key>frames</key>
<array>
<string>boyturnaround-000.png</string>
<string>boyturnaround-001.png</string>
<string>boyturnaround-002.png</string>
</array>
</dict>
</dict>
</plist>
请指导我如何按顺序加载动画?
I have a plist from which i am trying to access its animations in sequence (as defined in the PList) but when I store it in NSDictionary and then try to play animation one by one it is not playing them in sequence.
e.g,
int index = 0;
[self playAnimation:[animTypes objectAtIndex:index++]];
[self playAnimation:[animTypes objectAtIndex:index++]];
This is the code of my Plist:
<plist version="1.0">
<dict>
<key>Boy</key>
<dict>
<key>Enter</key>
<dict>
<key>disabled</key>
<false/>
<key>halfCycle</key>
<true/>
<key>fps</key>
<integer>1</integer>
<key>defaultFrame</key>
<integer>0</integer>
<key>useZwoptex</key>
<true/>
<key>frames</key>
<array>
<string>boy-000.png</string>
</array>
</dict>
<key>Jump</key>
<dict>
<key>disabled</key>
<false/>
<key>halfCycle</key>
<true/>
<key>fps</key>
<integer>13</integer>
<key>defaultFrame</key>
<integer>0</integer>
<key>useZwoptex</key>
<true/>
<key>frames</key>
<array>
<string>boyjump-000.png</string>
<string>boyjump-001.png</string>
<string>boyjump-002.png</string>
</array>
</dict>
<key>Turnaround</key>
<dict>
<key>disabled</key>
<false/>
<key>halfCycle</key>
<true/>
<key>fps</key>
<integer>21</integer>
<key>defaultFrame</key>
<integer>0</integer>
<key>useZwoptex</key>
<true/>
<key>frames</key>
<array>
<string>boyturnaround-000.png</string>
<string>boyturnaround-001.png</string>
<string>boyturnaround-002.png</string>
</array>
</dict>
</dict>
</plist>
Plz guide me how to load the animations in sequence?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,djhworld 是正确的 - NSDictionary 对象不会按照创建的顺序返回条目。
我对 Objective C 相当陌生,也遇到了同样的问题。我制定的解决方案允许我仍然使用单个 NSDictionary,但在使用它之前我根据键对条目进行排序。在 util 类中,我对键进行排序并返回一个排序数组:
然后我只需运行按顺序从字典中提取它们的键...
唯一需要注意的是,您必须使用正确排序的键。我采用了使用 nnn:keyName 形式的键的约定,因此我可以对它们进行排序,但仍然提供人类可读的名称。例如 001:处理 XYZ。
Yes, djhworld is correct - NSDictionary objects don't return entries in the order they're created.
I'm fairly new to Objective C and came up against the very same problem. The solution I cooked up allowed me to still use a single NSDictionary, but before using it I sort the entries based on the key. In a util class, I sort the keys and return a sorted array:
Then I just run through the keys pulling them from the Dictionary in order...
The only caveat is that you have to use keys that sort properly. I've adopted a convention of using keys in the form nnn:keyName, so I can sort them but still provide human-readable names. e.g. 001:process XYZ.
字典不像数组那样按顺序存储对象,而是存储键值对,因此结构实际上可以按任何顺序。
我建议将你的 NSDict 对象(定义你的动画序列)放在
NSArray
中Dictionarys don't store objects in order in a sequence like an array, they store key-value pairs so the structure could in effect be in any order.
I suggest putting your NSDict objects (that define your animation sequences) in an
NSArray