从 PList 读取字典时出现错误顺序

发布于 2024-09-07 18:40:45 字数 2373 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

肥爪爪 2024-09-14 18:40:45

是的,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。

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.

几度春秋 2024-09-14 18:40:45

字典不像数组那样按顺序存储对象,而是存储键值对,因此结构实际上可以按任何顺序。

我建议将你的 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>

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

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文