从PList读取字典时的错误顺序

时间:2010-07-02 08:09:04

标签: iphone cocos2d-iphone plist nsdictionary

我有一个plist,我试图按顺序访问它的动画(如PList中所定义)但是当我将它存储在NSDictionary中然后尝试逐个播放动画时它不是按顺序播放它们。 e.g,

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>

Plz指导我如何按顺序加载动画?

2 个答案:

答案 0 :(得分:5)

是的,djhworld是正确的 - NSDictionary对象不会按照创建的顺序返回条目。

我对Objective C很新,遇到了同样的问题。我编写的解决方案允许我仍然使用单个NSDictionary,但在使用它之前,我根据键对条目进行排序。在util类中,我对键进行排序并返回一个已排序的数组:

+(NSArray *) sortedKeysForDictionary:(NSDictionary *)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];

        // process the entry here

    }

}

唯一需要注意的是,您必须使用正确排序的键。我采用了一种使用nnn:keyName形式的键的约定,所以我可以对它们进行排序,但仍然提供人类可读的名称。例如001:处理XYZ。

答案 1 :(得分:2)

Dictionarys不按照数组的顺序按顺序存储对象,它们存储键值对,因此结构实际上可以是任何顺序。

我建议将您的NSDict对象(定义动画序列)放在NSArray

<plist version="1.0">
<array>
    <dict>
        <key>Name</key>
        <string>Boy</string>
        <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>
    <dict>
        <key>Name</key>
        <string>Jump</string>
        <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>
    <dict>
        <key>Name</key>
        <string>Enter</string>
        <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>
</array>
</plist>