从PList中阅读Dictionary时出现错序 [英] False order when reading Dictionary from PList
问题描述
我有一个plist,从我试图访问它的动画序列(在PList中定义),但当我存储在NSDictionary,然后尝试一个一个播放动画,它不是按顺序播放它们。
eg,
int index = 0;
[self playAnimation:[animTypes objectAtIndex:index ++]];
[self playAnimation:[animTypes objectAtIndex:index ++]];
这是我的Plist的代码:
< plist version =1.0>
< dict>
< key> Boy< / key>
< dict>
< key>输入< / key>
< dict>
< key>已停用< / 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>已停用< / key>
< 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>周转< / key>
< dict>
< key>已停用< / 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指导我如何按顺序加载动画?
是的,djhworld是正确的 - NSDictionary对象不会按照创建的顺序返回条目。 >
我对Objective C很新,并且遇到了同样的问题。我熟悉的解决方案允许我仍然使用单个NSDictionary,但在使用它之前,我基于键排序条目。在util类中,我对键进行排序并返回一个有序数组:
+(NSArray *)sortedKeysForDictionary: dict {
NSArray * keys = [dict allKeys];
NSMutableArray * anArray = [NSMutableArray arrayWithArray:keys];
keys = [anArray sortedArrayUsingSelector:@selector(localizedCompare :)];
return keys;
}
然后我只是通过键从字典中拉出来。
- (void)processEntries {
NSArray * keys = [Utils sortedKeysForDictionary:myDictionary];
for(int i = 0; i< keys.count; i ++){
NSString * key = [keys objectAtIndex:i];
id dictEntry = [myDictionary valueForKey:key];
//处理此处的条目
}
}
唯一的警告是你必须使用正确排序的键。我采用了以nnn:keyName形式使用键的惯例,因此我可以对它们进行排序,但仍然提供人类可读的名称。例如001:进程XYZ。
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?
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:
+(NSArray *) sortedKeysForDictionary:(NSDictionary *)dict {
NSArray *keys = [dict allKeys];
NSMutableArray *anArray = [NSMutableArray arrayWithArray:keys];
keys = [anArray sortedArrayUsingSelector:@selector(localizedCompare:)];
return keys;
}
Then I just run through the keys pulling them from the Dictionary in order...
-(void) processEntries {
NSArray *keys = [Utils sortedKeysForDictionary:myDictionary];
for (int i=0; i<keys.count; i++){
NSString *key = [keys objectAtIndex:i];
id dictEntry = [myDictionary valueForKey:key];
// process the entry here
}
}
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.
这篇关于从PList中阅读Dictionary时出现错序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!